zoukankan      html  css  js  c++  java
  • IOS基础-设计模式、手势识别器

    target/action设计模式

    每个视图点击事件都不一样,我们无法预先知道这个视图点击之后都要实现什么效果,因此我们在类内部提前写好点击事件不科学

    使用target/action实现解耦

    @implementation myView
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"整个人都不好了");
        //此时,不确定有那个对象实现action方法,
        //也不确定执行什么方法,这样可以达到解耦的目的,即:实现触摸动作和触摸之后做什么操作完全分开
        [self.target performSelector:self.action withObject:self afterDelay:0];
    }
    @end
    
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"整个人都不好了");
        //此时,不确定有那个对象实现action方法,
        //也不确定执行什么方法,这样可以达到解耦的目的,即:实现触摸动作和触摸之后做什么操作完全分开
        [self.target performSelector:self.action withObject:self afterDelay:0];
    }
    @end
    
    
    
    
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor colorWithRed:0 green:49/255.0 blue:79/255.0 alpha:1];
        myView *view = [[myView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        view.backgroundColor = [UIColor colorWithRed:113/255.0 green:150/255.0 blue:159/255.0 alpha:1];
        view.layer.cornerRadius = 8;
        [self.view addSubview:view];
        view.tag = 100;
        //设置view的目标对象为控制器(self)
        view.target =self;
        //设置view要执行的方法(行为);
        view.action = @selector(changeColor);
    

    手势识别器

    手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们把重心放在识别之后要做什么操作上

    手势识别器是iOS中比较抽象的一个类,用于识别一个手势,即 有规律的触摸

    #import "PhotoViewController.h"
    #define kImageCount 6
    @interface PhotoViewController ()
    
    {
        int _currentIndex;//当前图片索引
    }
    
    
    
    
    
    @end
    
    @implementation PhotoViewController
    
    - (void)dealloc{
        [_imageView release];
        [super dealloc];
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self initLayout];
        [self addGesture];
        
    }
    
    
    #pragma mark  布局
    - (void)initLayout{
        /*添加图片展示控件*/
        CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
        CGFloat topPadding = 20;
        CGFloat y = 22 + 44 +topPadding;
        CGFloat height = screenSize.height - y - topPadding;
        
        CGRect imageFrame = CGRectMake(0, y, screenSize.width, height);
        _imageView = [[UIImageView alloc] initWithFrame:imageFrame];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;//设置内容模式为缩放填充
        _imageView.userInteractionEnabled = YES;//这里必须设置为yes,否则无法接收手势操作
        [self.view addSubview:_imageView];
        
        //添加默认图片
        UIImage *image = [UIImage imageNamed:@"0.png"];
        _imageView.image = image;
        [self showPhotoName];
        
    }
    
    
    
    
    #pragma mark  添加手势
    - (void)addGesture{
        /*1.添加点按手势*/
        //创建手势对象
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
        //设置手势属性
        tapGesture.numberOfTapsRequired = 1;//设置点按次数,默认为1,注意在iOS中很少用到双击操作
        tapGesture.numberOfTouchesRequired = 1;//点按的手指数
        //添加手势到对象(注意,这里添加到控制器视图中,而不是图片上,否则点击空白无法隐藏导航栏)
        [self.view addGestureRecognizer:tapGesture];
        
        /*2.添加长按手势*/
        //创建手势对象
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
        longPressGesture.minimumPressDuration = 0.5;//设置时长,默认0.5
        //注意由于是长按图片操作,所以手势添加在图片,而不是视图控制器上
        [_imageView addGestureRecognizer:longPressGesture];
        
        /*3.添加捏合手势*/
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [self.view addGestureRecognizer:pinchGesture];
        
        /*4.添加旋转手势*/
        UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
        [self.view addGestureRecognizer:rotationGesture];
        
        /*5.添加拖动手势*/
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        [self.view addGestureRecognizer:panGesture];
        
    //    6.添加清扫手势
        UISwipeGestureRecognizer *swipeGestureToRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        swipeGestureToRight.direction = UISwipeGestureRecognizerDirectionRight;//默认是向右轻扫
        [self.view addGestureRecognizer:swipeGestureToRight];
        
        UISwipeGestureRecognizer *swipeGestureToLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        swipeGestureToLeft.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:swipeGestureToLeft];
        
    //    解决在图片上滑动时拖动手势和轻扫手势的冲突
        [panGesture requireGestureRecognizerToFail:swipeGestureToRight];
        [panGesture requireGestureRecognizerToFail:swipeGestureToLeft];
        
        //解决拖动和长按手势之间的冲突
        [longPressGesture requireGestureRecognizerToFail:panGesture];
        
        
    }
    
    
    
    #pragma mark  显示图片名称
    - (void)showPhotoName{
        NSString *title = [NSString stringWithFormat:@"%i.png",_currentIndex];
        [self setTitle:title];
    }
    
    #pragma mark  下一张图片
    - (void)nextImage{
        int index = (_currentIndex + kImageCount + 1) % kImageCount;
        NSString *imageName = [NSString stringWithFormat:@"%i.png",index];
        _imageView.image = [UIImage imageNamed:imageName];
        _currentIndex = index;
        [self showPhotoName];
    }
    
    
    
    
    
    
    #pragma mark  上一张图片
    - (void)lastImage{
        int index = (_currentIndex + kImageCount - 1) % kImageCount;
        NSString *imageName = [NSString stringWithFormat:@"%i.png",index];
        _imageView.image = [UIImage imageNamed:imageName];
        [self showPhotoName];
    }
    
    
    
    
    #pragma mark  - 手势操作
    #pragma mark   点按隐藏或者显示导航栏
    - (void)tapImage:(UITapGestureRecognizer *)gesture{
        NSLog(@"点按隐藏或显示导航");
        BOOL hidden = !self.navigationController.navigationBarHidden;
        [self.navigationController setNavigationBarHidden:hidden animated:YES];
    }
    
    #pragma mark  长按显示是否删除
    - (void)longPressGesture:(UILongPressGestureRecognizer *)gesture{
        NSLog(@"长按删除");
        //注意在手势里面有一个view属性可以获取点按的视图
    //    UIImageView *imageView = (UIImageView *)gesture.view;
        
        if (gesture.state == UIGestureRecognizerStateBegan) {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"系统提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除图片" otherButtonTitles: nil];
            [actionSheet showInView:self.view];
        }
    }
    
    #pragma mark 捏合时缩放图片
    - (void)pinchGesture:(UIPinchGestureRecognizer *)gesture{
        NSLog(@"捏合缩放图片");
        if (gesture.state == UIGestureRecognizerStateChanged) {
            //捏合手势scale属性记录的缩放比例
            _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
        }
        else if(gesture.state == UIGestureRecognizerStateEnded){
            [UIView animateWithDuration:.5 animations:^{
                _imageView.transform = CGAffineTransformIdentity;//取消一切形变
            }];
        }
    }
    
    #pragma mark  旋转图片
    - (void)rotationGesture:(UIRotationGestureRecognizer *)gesture{
        NSLog(@"旋转跳跃");
        if (gesture.state == UIGestureRecognizerStateChanged) {
            //旋转手势中rotation属性记录了旋转弧度
            _imageView.transform = CGAffineTransformMakeRotation(gesture.rotation) ;
        }else if(gesture.state == UIGestureRecognizerStateEnded){
            [UIView animateWithDuration:.8 animations:^{
                _imageView.transform = CGAffineTransformIdentity;//取消形变
            }];
        }
        
    }
    
    #pragma  mark  拖动图片
    - (void)panGesture:(UIPanGestureRecognizer *)gesture{
        NSLog(@"拖动图片");
        if (gesture.state == UIGestureRecognizerStateChanged) {
            CGPoint transLocation = [gesture translationInView:self.view];
            _imageView.transform = CGAffineTransformMakeTranslation(transLocation.x, transLocation.y);
            NSLog(@"transLoaction.x =%f ",transLocation.x);
        }else if(gesture.state == UIGestureRecognizerStateEnded){
            CGPoint transLocation = [gesture translationInView:self.view];
            if (transLocation.x > 150) {
                [self nextImage];
                [UIView animateWithDuration:.5 animations:^{
                    _imageView.transform = CGAffineTransformIdentity;
                }];
            }else
            [UIView animateWithDuration:.5 animations:^{
                _imageView.transform = CGAffineTransformIdentity;
            }];
            if (transLocation.x < -150) {
                [self lastImage];
                [UIView animateWithDuration:.5 animations:^{
                    _imageView.transform = CGAffineTransformIdentity;
                }];
            }else
                [UIView animateWithDuration:.5 animations:^{
                    _imageView.transform = CGAffineTransformIdentity;
                }];
        }
    }
    
    #pragma mark  轻扫查看上一张或下一张
    - (void)swipeGesture:(UISwipeGestureRecognizer *)gesture{
        //注意,轻扫是连续手势,但是只有在识别结束后才触发,不用判断状态
        if (gesture.state == UIGestureRecognizerStateEnded) {
            if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
                [self nextImage];
                NSLog(@"right");
            }else if(gesture.direction == UISwipeGestureRecognizerDirectionLeft){
                [self lastImage];
                NSLog(@"left");
            }
        }
    }
    
  • 相关阅读:
    HashMap 实现原理
    王东江网站
    网站建设
    mysql 查询 执行流程
    两个线程交替打印1到100
    三个线程交替打印十次ABC
    Java动态链接是什么意思
    双亲委派机制
    笔记
    redis集群搭建
  • 原文地址:https://www.cnblogs.com/dingjianjaja/p/4840800.html
Copyright © 2011-2022 走看看