编程技巧 - 3
2015-08-18 20:48
537 查看
18.封装UIButton的初始化 - 简化:
使用:
是不是轻巧多啦
19.UIControlState 还能这样用
那如果想在事件:UIControlEventTouchDown 或其他情况使用状态效果,就可以强制转换一下。
20.更新约束放在updateConstraints里面
21.高宽增加用sizeOffset
22.Masonry left right并写
23.添加手势的优雅写法
添加长按和触击用这样的一句话讲完,又优雅又简洁。
24.取Rect值
一个视图存在一个CGRect属性,当我们想取它的最大的X如图:
我们可以用:CGRectGetMaxX这个宏来获取
同理,我们也能获取最小,中间值等,也可以获取Y的值
还有一个相当好的宏方法MAX(A, B),这个方法很灵活,如下面,配合着使用:
25.封装技巧
直接上一段代码:
这里做个提醒:前面的代码有一处是:self.delegate = self ;
优雅! 内含乾坤!
- (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];
优雅! 内含乾坤!
相关文章推荐
- autolayout的使用原理及代码实现
- Java注解
- Java中我常使用的ArrayList方法
- Java深度历险(五)Java泛型
- python *和**
- 指针和引用区别(More Effective_C++_1(基础))
- 修改Delphi工具控件的默认字体
- 简单图书馆系统(面向对象编程思想)
- 全新编程模式---站立编程--独创编程方式,可能会流行
- C++ Primer 5e chapter 1
- Java深度历险(四)Java垃圾回收机制与引用类型
- 2.10 寻找数组中的最大值和最小值
- Java基础---包.内部类
- coredata使用代码实现
- Java深度历险(二)Java类的加载、链接和初始化
- Java基础---static关键字
- php json_decode 不支持的模式
- Mac自带Python版本升级
- python 使用pymsql连接数据库
- C# WPF 动态添加窗体