UI控件与Block的灵活使用
来源:互联网 发布:macbook有的软件删不掉 编辑:程序博客网 时间:2024/05/16 17:11
static char * const UIViewBlockKey = "UIHelperViewBlockKey";
+ (UIView *) viewWithtouchesBegan:( void(^)(NSSet *touches, UIEvent *event) ) touchesBeganBlock
touchesMoved:( void(^)(NSSet *touches, UIEvent *event) ) touchesMovedBlock
touchesEnded:( void(^)(NSSet *touches, UIEvent *event) ) touchesEndedBlock
touchesCancelled:( void(^)(NSSet *touches, UIEvent *event) ) touchesCancelledBlock;
@end
@implementation UIView (UIHelper)
+ (UIView *) viewWithtouchesBegan:( void(^)(NSSet *touches, UIEvent *event) ) touchesBeganBlock
touchesMoved:( void(^)(NSSet *touches, UIEvent *event) ) touchesMovedBlock
touchesEnded:( void(^)(NSSet *touches, UIEvent *event) ) touchesEndedBlock
touchesCancelled:( void(^)(NSSet *touches, UIEvent *event) ) touchesCancelledBlock
{
UIView *theView = [[UIView alloc] init];
NSArray *opreations = (NSArray *)objc_getAssociatedObject(theView, UIViewBlockKey);
if (opreations == nil)
{
opreations = [[NSArray arrayWithObjects:touchesBeganBlock, touchesMovedBlock, touchesEndedBlock, touchesCancelledBlock, nil] retain];
objc_setAssociatedObject(theView, UIViewBlockKey, opreations, OBJC_ASSOCIATION_RETAIN);
}
return [theView autorelease];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *opreations = (NSArray *)objc_getAssociatedObject(self, UIViewBlockKey);
void(^handle)(NSSet *touches, UIEvent *event) = [opreations objectAtIndex:0];
if (handle == nil)
{
return;
}
handle(touches, event);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *opreations = (NSArray *)objc_getAssociatedObject(self, UIViewBlockKey);
void(^handle)(NSSet *touches, UIEvent *event) = [opreations objectAtIndex:1];
if (handle == nil)
{
return;
}
handle(touches, event);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *opreations = (NSArray *)objc_getAssociatedObject(self, UIViewBlockKey);
void(^handle)(NSSet *touches, UIEvent *event) = [opreations objectAtIndex:2];
if (handle == nil)
{
return;
}
handle(touches, event);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *opreations = (NSArray *)objc_getAssociatedObject(self, UIViewBlockKey);
void(^handle)(NSSet *touches, UIEvent *event) = [opreations objectAtIndex:3];
if (handle == nil)
{
return;
}
handle(touches, event);
}
@end
//使用方法***************
UIView *aView = [UIView viewWithtouchesBegan:^(NSSet *touches, UIEvent *event) {
NSLog(@"touchesBegan");
} touchesMoved:^(NSSet *touches, UIEvent *event) {
NSLog(@"touchesMoved");
} touchesEnded:^(NSSet *touches, UIEvent *event) {
NSLog(@"touchesEnded");
} touchesCancelled:^(NSSet *touches, UIEvent *event) {
NSLog(@"touchesCancelled");
}];
aView.frame = CGRectMake(100, 0, 100, 44);
aView.backgroundColor = [UIColor redColor];
[self.window addSubview:aView];
- UI控件与Block的灵活使用
- iOS笔记UI--UI控件的灵活运用
- 如何使用Fragment构建灵活的UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用(二)--构建灵活的Android应用UI
- Fragment的概念与使用--构建灵活的Android应用UI
- 构建灵活的UI
- 灵活使用控件
- 使用约束布局(ConstraintLayout)构建灵活的UI
- 使用约束布局(ConstraintLayout)构建灵活的UI
- python字典与数据库的灵活使用
- 关于CDHtmlDialog的控制。MFC Web控件的灵活使用。
- 关于CDHtmlDialog的控制。MFC Web控件的灵活使用
- IOS的UI控件使用
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 3 - 创建灵活可变的UI ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 3 - 创建灵活可变的UI ]
- 系统视图sys.databases的collation_name 列为 NULL的问题
- Asterisk文件目录
- Android快速启动要点总结
- c程序内存分配及全局变量内存分配
- 如何快速配好java环境变量和查看电脑上安装JDK的版本位数
- UI控件与Block的灵活使用
- 期货结算账户
- sqlserver 数据横向查询和游标使用
- 在ASP.NET 2.0中实现URL重写
- SATA硬盘和IDE硬盘区别及优劣比较
- SQLSERVER批量删除数据库中的表或者存储过程
- 从设备上下文中得到位图像素
- 10款非Windows免费操作系统推介
- JS写怀旧小游戏系列(一)俄罗斯方块