您的位置:首页 > 编程语言

编程技巧 - 3

2015-08-18 20:48 537 查看
18.封装UIButton的初始化 - 简化:

- (UIButton *)buttonWith:(NSString *)noraml hightLight:(NSString *)hightLight action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:noraml] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:hightLight] forState:UIControlStateHighlighted];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}


使用:

self.voiceBtn = [self buttonWith:@"chat_bottom_voice_nor.png"
                      hightLight:@"chat_bottom_voice_press.png"
                          action:@selector(voiceBtnPress:)];
[self.voiceBtn setFrame:CGRectMake(0,0, 33, 33)];


是不是轻巧多啦

19.UIControlState 还能这样用

[self.speakButton setTitleColor:[UIColor redColor] forState:(UIControlState)UIControlEventTouchDown];
因为传统的UIControlState只有下面几种形式:

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};


那如果想在事件:UIControlEventTouchDown 或其他情况使用状态效果,就可以强制转换一下。

20.更新约束放在updateConstraints里面

- (void)updateConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self);
        make.height.lessThanOrEqualTo(self);
    }];

    //according to apple super should be called at end of method
    [super updateConstraints];
}


21.高宽增加用sizeOffset

// make width = superview.width + 100, height = superview.height - 50
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))


22.Masonry left right并写

[subv mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.and.right.equalTo(container);

}];
left.and.right 一起写,多好!

23.添加手势的优雅写法

添加长按和触击用这样的一句话讲完,又优雅又简洁。

[self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];

        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPress:)]];


24.取Rect值

一个视图存在一个CGRect属性,当我们想取它的最大的X如图:



我们可以用:CGRectGetMaxX这个宏来获取

CGFloat contentX = CGRectGetMaxX(self.iconRect) + kIconMarginX;


同理,我们也能获取最小,中间值等,也可以获取Y的值

还有一个相当好的宏方法MAX(A, B),这个方法很灵活,如下面,配合着使用:

self.cellHeight = MAX(CGRectGetMaxY(self.iconRect), CGRectGetMaxY(self.chartViewRect)) + kIconMarginY


25.封装技巧

直接上一段代码:

typedef enum
{
    JSMessagesViewAvatarPolicyIncomingOnly = 0,
    JSMessagesViewAvatarPolicyBoth,
    JSMessagesViewAvatarPolicyNone
}   JSMessagesViewAvatarPolicy;


- (JSMessagesViewAvatarPolicy)avatarPolicy
{

    return JSMessagesViewAvatarPolicyBoth;
}


这里做个提醒:前面的代码有一处是:self.delegate = self ;
- (BOOL)shouldHaveAvatarForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([self.delegate avatarPolicy]) {
        case JSMessagesViewAvatarPolicyIncomingOnly:
            return [self.delegate messageTypeForRowAtIndexPath:indexPath] == JSBubbleMessageTypeIncoming;
            
        case JSMessagesViewAvatarPolicyBoth:
            return YES;
            
        case JSMessagesViewAvatarPolicyNone:
        default:
            return NO;
    }
}


BOOL hasAvatar = [self shouldHaveAvatarForRowAtIndexPath:indexPath];
    
NSString *CellID = [NSString stringWithFormat:@"MessageCell_%d_%d_%d_%d", type, bubbleStyle, hasTimestamp, hasAvatar];


优雅! 内含乾坤!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: