UI-设计模式 手势处理
发布日期:2022-02-08 18:03:24 浏览次数:51 分类:技术文章

本文共 6210 字,大约阅读时间需要 20 分钟。

1. 设计模式:

面向对象的编程核心思想:高内聚 低耦合

使用target action实现解耦

//MyButton.h文件 用UIView模拟一个按钮#import 
@interface MyButton : UIView{ id _target; SEL _action;}- (void)addMyTarget:(id)target action:(SEL)action;@end//MyButton.m文件#import "MyButton.h"@implementation MyButton-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_target performSelector:_action];}- (void)addMyTarget:(id)target action:(SEL)action { _target = target; // self _action = action; // @selector:}@end
_target performSelector:_action : _target调用_action方法  (_action 的类型是SEL )

2. 代理模式:

先在某个类中写协议(或者建一个protocol文件),然后在该类中调用协议的方法(调用的时候会跳到控制器)。。

再然后执行一下三步:

三步: 遵守协议(控制器遵守)---->设置代理(设置代理为控制器)---->实现方法(在控制器中实现方法)

3. UIImageView:

- (void)addAllViews {#pragma ===== 正常添加--显示图片 =====    self.backgroundColor = [UIColor whiteColor];        UIImageView *imV1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 300, 200)];    UIImageView *imV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 300, 200)];    imV.backgroundColor = [UIColor cyanColor];    // 寻找图片的第一种格式:直接使用imageNamed    // 如果图片格式是png的,可以省略后面的.png 直接写图片的名字即可    imV.image = [UIImage imageNamed:@"1.jpeg"];            // 寻找图片的第二种格式:使用imageWithContentsOfFile    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpeg"];    // 或者是:pathForResource:@"2.jpeg"  ofType:nil    imV1.image = [UIImage imageWithContentsOfFile:path];// 然后将路径赋值            #pragma ===== 动态图 =====    UIImage *img1 = [UIImage imageNamed:@"1.tiff"];    UIImage *img2 = [UIImage imageNamed:@"2.tiff"];    UIImage *img3 = [UIImage imageNamed:@"3.tiff"];    UIImage *img4 = [UIImage imageNamed:@"4.tiff"];    UIImage *img5 = [UIImage imageNamed:@"5.tiff"];    UIImage *img6 = [UIImage imageNamed:@"6.tiff"];    UIImage *img7 = [UIImage imageNamed:@"7.tiff"];    // 1. 动图需要的图片: 用animationImages---是个数组    imV.animationImages = @[img1,img2,img3,img4,img5,img6,img7];            // 动图循环次数 :animationRepeatCount    imV.animationRepeatCount = 2563456;        // 每次动图时间 duration    imV.animationDuration = 0.8;        //让动画开始的方法    [imV startAnimating];        // 动画结束//    [imV stopAnimating];        //意思是如果在图片上面加了例如button这样需要相应的东西,就一定要把imageViewde userInteractionEnabled设置为YES;    imV.userInteractionEnabled = YES; // imageView默认的userInteractionEnabled是NO,会阻断响应链    [self addSubview:imV];    [self addSubview:imV1];    }

4. 手势处理:

包括:

1> 轻拍:UITapGestureRecognizer

2> 长按:UILongPressGestureRecognizer

3> 旋转:UIRotationGestureRecognizer

4> 捏合:UIPinchGestureRecognizer

5> 屏幕边缘清扫:UIScreenEdgePanGestureRecognizer

6> 平移:UIPanGestureRecognizer

7> 清扫:UISwipeGestureRecognizer

- (void)addAllViews {    self.backgroundColor = [UIColor whiteColor];    self.img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpeg"]];    self.img.frame = [UIScreen mainScreen].bounds;    [self addSubview:self.img];        #pragma mark=============手势:===========    #pragma----- 1. 轻拍:    /*         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];    [self.img addGestureRecognizer:tap];    // 切记:切记:!!! 要把这个userInteractionEnabled打开    // 因为imageView会阻断事件,所以在添加手势时,要把这个userInteractionEnabled打开(打开交互)    self.img.userInteractionEnabled = YES;        // 点击次数    tap.numberOfTapsRequired = 2;    //    // 同时有几个触摸点(几个指头触摸)//    tap.numberOfTouchesRequired = 2;          */    #pragma----- 2. 长按:        /*    UILongPressGestureRecognizer *lo = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];    self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:lo];     */        #pragma----- 3. 旋转:         /*    UIRotationGestureRecognizer *ro = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];     self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:ro];      */             #pragma----- 4. 捏合    /*    UIPinchGestureRecognizer *pin= [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];    self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:pin];     */    #pragma----- 5. 屏幕边缘清扫    /*    UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction)];     self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:edge];     */    #pragma----- 6. 平移手势             UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];    self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:pan];                 #pragma----- 7. 清扫手势    /*         UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];    swipe.direction = UISwipeGestureRecognizerDirectionLeft;    swipe.direction = UISwipeGestureRecognizerDirectionRight;     self.img.userInteractionEnabled = YES;    [self.img addGestureRecognizer:swipe];          */    }- (void)tapAction {    NSLog(@"轻拍~ ~ ~");}- (void)longAction:(UILongPressGestureRecognizer *)longG {    if (longG.state == UIGestureRecognizerStateBegan) {        // 如果不添加这个判断条件 会打印两次结果:一次是开始 一次是结束         NSLog(@"长按~ ~ ~");    }}- (void)rotationAction:(UIRotationGestureRecognizer *)sender {    self.img.transform = CGAffineTransformRotate(self.img.transform, sender.rotation);    // 把之前一次记录旋转的角度清空    [sender setRotation:0];// 清空    NSLog(@"旋转 ....");    }- (void)pinchAction:(UIPinchGestureRecognizer *)sender {    self.img.transform = CGAffineTransformScale(self.img.transform, sender.scale, sender.scale);    [sender setScale:1]; // 清空    NSLog(@"pinch.....");}- (void)edgeAction {    NSLog(@"edge....");}- (void)panAction:(UIPanGestureRecognizer *)sender {    NSLog(@"pan....");    CGPoint point = [sender translationInView:self.img];// 获取点的新信息        self.img.transform = CGAffineTransformTranslate(self.img.transform, point.x, point.y );        // 把每次保存的之前一次的移动距离清空    [sender setTranslation:CGPointZero inView:self.img];    // 每次都会回到原点再进行平移//    self.img.transform = CGAffineTransformMakeTranslation(point.x, point.y);}- (void)swipeAction {    NSLog(@"swipe....");}
View的transform的属性可以平移 旋转 缩放.....

记得需要清空上一次的记录

转载地址:https://blog.csdn.net/Evelynzn/article/details/49206649 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UI-事件处理
下一篇:UIControl及其子类

发表评论

最新留言

很好
[***.229.124.182]2024年03月24日 14时02分31秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

洞泾智能机器人产业基地_G60科创走廊洞泾人工智能产业基地(核心区块)暨洞泾镇招商人员培训班顺利开班... 2019-04-21
java 拼接路径优雅方式_Java安全编码实践总结 2019-04-21
realme x2 深度测试打不开_搭载65W超级闪充,realme真我X7手机充电评测 2019-04-21
整数取反编程_【每日编程185期】数字的补数 2019-04-21
能用别的软件吗_手机软件能用蓝牙传送吗 2019-04-21
为什么图片要2的倍数_为什么宝宝喜欢流“口水”?这种2种原因父母要知道,建议收藏... 2019-04-21
下载了XAMPP怎样打开MYSQL_xampp mysql安装启动 2019-04-21
pdo转mysql_mysql转mysqli或pdo 2019-04-21
mysql如果没有表就创建_mysql – 改变表是否存在或创建如果没有 2019-04-21
ireport连接mysql_ireport 4.5教程之数据源介绍 2019-04-21
mysql多维模型_数据仓库数据库设计方法---关系模型和多维模型比较分析 2019-04-21
局域网聊天程序 java MySQL_java 基于TCP/IP协议的局域网聊天小程序 2019-04-21
r glm 中的p值_假设检验中的P值 2019-04-21
mysql中sql语句结构_MySQL中使用sql语句获得表结构 2019-04-21
如何增加mysql主键约束_mysql修改表时怎么添加主键约束? 2019-04-21
java选择路径窗口_Java实现选择电脑路径的方法 2019-04-21
java 图像渐变_Java基础之在窗口中绘图——渐变填充(GradientApplet 1) 2019-04-21
冒泡排序面向对象java_所谓的面向对象实现的冒泡排序 2019-04-21
proto 客户端 JAVA_Kubernetes官方java客户端之五:proto基本操作 2019-04-21
java编写roguelike_RogueLike地牢生成算法Unity实现 2019-04-21