Nov 16, 2015 - 扩展uibutton点击区域

Comments

扩展UIButton的点击区域

//
//  UIButton+EnlargeEdge.m
//  buttonDemo
//
//  Created by dengyonghao on 15/11/16.
//  Copyright © 2015年 dengyonghao. All rights reserved.
//

#import "UIButton+EnlargeEdge.h"
#import <objc/runtime.h>

#define keyTopEdge  @"keyTopEdge"
#define keyleftEdge  @"keyTopEdge"
#define keybottomEdge  @"keyTopEdge"
#define keyrightEdge  @"keyTopEdge"

@implementation UIButton (EnlargeEdge)

- (void)setEnlargeEdge:(CGFloat) size
{
    objc_setAssociatedObject(self, keyTopEdge, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keyleftEdge, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keybottomEdge, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keyrightEdge, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left
{
    objc_setAssociatedObject(self, keyTopEdge, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keyleftEdge, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keybottomEdge, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, keyrightEdge, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

//使用bounds来增大点击区域
- (CGRect)enlargedRect
{
    NSNumber* topEdge = objc_getAssociatedObject(self, keyTopEdge);
    NSNumber* leftEdge = objc_getAssociatedObject(self, keyleftEdge);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, keybottomEdge);
     NSNumber* rightEdge = objc_getAssociatedObject(self, keyrightEdge);

    if (topEdge && rightEdge && bottomEdge && leftEdge)
    {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super pointInside:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? YES : NO;
}

@end

Sep 2, 2015 - Masonry的使用

Comments

Masonry的使用

Masonry介绍

  Masonry是一个轻量级的布局框架,对开纯手写代码的我来说,它提供的帮助实在太大了,在此记录下。
先上源码:https://github.com/Masonry/Masonry

Masonry基本属性

@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基线

Masonry中的约束函数

//新增约束,Autolayout不能同时存在两条针对于同一对象的约束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;     

//更新约束
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;     

//删除约束
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

Masonry中约束用法

//详细例子请查看Masonry里面的Examples
__weak ViewController *weakself = self;

UIView *view = [UIView new];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];

[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(weakself.view);//设置中心点
    make.size.mas_equalTo(CGSizeMake(300, 300));//设置大小
}];

Sep 1, 2015 - Core_graphics使用

Comments

core graphics使用

五步曲

1. 获取上下文

CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文

2. 创建路径并添加到上下文中

CGMutablePathRef path = CGPathCreateMutable();//创建路径

3. 进行绘图内容的设置

CGPathMoveToPoint(path, nil, 50, 50);//设置路径起点
CGPathAddLineToPoint(path, nil, 50, 100);//绘制直线(可以根据实际情况选择)
CGContextAddPath(context, path);//把路径添加到上下文中

//设置图形上下文状态属性
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1);//设置笔触颜色
CGContextSetRGBFillColor(context, 0, 1.0, 0, 1);//设置填充色
CGContextSetLineWidth(context, 1.0);//设置线条宽度
CGContextSetLineCap(context, kCGLineCapRound);//设置顶点样式
CGContextSetLineJoin(context, kCGLineJoinRound);//设置连接点样式
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 3, [UIColor blackColor].CGColor);//设置阴影(详细参数查看api解释)
......

4. 开始绘图(CGContextDrawPath)

CGContextDrawPath(context, kCGPathFillStroke);//最后一个参数是填充类型

5. 释放路径(CGPathRelease)

CGPathRelease(path);//不在ARC的管理范围之内