DelegateAndGestureRecognizer 手势识别

Delegate And GestureRecognizer

修改填充模式

 imageView.contentMode = UIViewContentModeScaleAspectFit; 

手势, 对触摸的封装

    UIGestureRecognizer, 手势类, 内部封装的模式target-action, 继承于NSObject

    UIGestureRecognizer, 是一个抽象基类, 不能够直接创建对象, 需要先子类化, 再通过子类创建对象

    UIGestureRecognizer的子类:

    UITapGestureRecognizer, 点击手势

    UIPinchGestureRecognizer, 捏合手势

    UIRotationGestureRecognizer, 旋转手势

    UISwipeGestureRecognizer, 轻扫手势

    UIPanGestureRecognizer, 平移手势

    UIScreenEdgePanGestureRecognizer, 边缘平移手势

    UILongPressGestureRecognizer, 长按手势

点击手势

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    //点击次数, 默认1
    tap.numberOfTapsRequired = 2;
    //触摸点的个数, 默认1
    tap.numberOfTouchesRequired = 2;
    //添加手势
    [imageView addGestureRecognizer:tap];
    [tap release];

       flag = YES;

- (void)tap {
    NSLog(@"%s", __FUNCTION__);
    //1
//    if (flag) {
//        imageView.image = [UIImage imageNamed:@"0.jpg"];
//        flag = NO;
//    } else {
//        imageView.image = [UIImage imageNamed:@"2.jpeg"];
//        flag = YES;
//    }
//
    //2
    imageView.image = flag ?  [UIImage imageNamed:@"0.jpg"] : [UIImage imageNamed:@"2.jpeg"];
    flag = !flag;  
}

捏合

  UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];
- (void)pinch:(UIPinchGestureRecognizer *)gesture {
//    NSLog(@"%s", __FUNCTION__);
    //缩放比例, >1 放大, <1 缩小
    NSLog(@"%.2f", gesture.scale);
    //变形(旋转, 缩放, 平移)
    imageView.transform = CGAffineTransformScale(imageView.transform, gesture.scale , gesture.scale);
    gesture.scale = 1;
}

旋转手势

 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
    [imageView addGestureRecognizer:rotation];
    [rotation release];
- (void)rotate:(UIRotationGestureRecognizer *)rotation {
    NSLog(@"%.2f", rotation.rotation);
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
    rotation.rotation = 0;

轻扫手势

  array = [[NSArray alloc]initWithObjects:@"0.jpg", @"1.jpg", @"2.jpeg", @"3.jpg", nil];
    index = 0;
    
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipeLeft];
    [swipeLeft release];
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [imageView addGestureRecognizer:swipeRight];
    [swipeRight release];
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"%ld", swipe.direction);
    NSLog(@"swipe");
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        //向右
        index--;
        if (index == -1) {
            index = 3;
        }
    } else {
        //向左
        index++;
        if (index == 4) {
            index = 0;
        }
    }
    imageView.image = [UIImage imageNamed:array[index]];
}


平移手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [imageView addGestureRecognizer:pan];
    [pan release];
- (void)pan:(UIPanGestureRecognizer *)pan {
    NSLog(@"pan");
    CGPoint point = [pan translationInView:self.view];
    NSLog(@"%@", NSStringFromCGPoint(point));
    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
    //CGPointZero
    //CGSizeZero
    //CGRectZero
    [pan setTranslation:CGPointZero inView:self.view];
}

边缘平移手势

 UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan:)];
    //边缘
    edgePan.edges = UIRectEdgeLeft;
    [imageView addGestureRecognizer:edgePan];
    [edgePan release];

长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [imageView addGestureRecognizer:longPress];
    [longPress release];
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
        UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    if (longPress.state == UIGestureRecognizerStateChanged) {
        NSLog(@"长按移动");
    }
    if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
   contextInfo:(void *)contextInfo {
    NSLog(@"保存完成");
    //警告框
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"图片已保存" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    //显示警告框
    [alertView show];
    [alertView release];
}

Delegate 模式

FirstViewController.m
#import "FirstViewController.h"
#import "TouchView.h"
#import "TouchViewPro.h"

@interface FirstViewController ()<TouchViewProDelegate> {
    TouchViewPro *touchViewPro;
}

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.997 blue:0.510 alpha:1.000];
    
    TouchView *touchView = [[TouchView alloc] initWithFrame:CGRectMake(130, 100, 100, 100)];
    touchView.backgroundColor = [UIColor purpleColor];
    [touchView addTarget:self action:@selector(touch:)];
    [self.view addSubview:touchView];
    [touchView release];
    
    //delegate, 代理模式
    
    //成为代理的类(FirstViewController)
    //1.遵守协议
    //2.实现协议中的方法
    
    touchViewPro = [[TouchViewPro alloc] initWithFrame:CGRectMake(220, 240, 100, 100)];
    touchViewPro.backgroundColor = [UIColor colorWithRed:0.555 green:1.000 blue:0.672 alpha:1.000];
    touchViewPro.delegate = self;
    [self.view addSubview:touchViewPro];
    [touchViewPro release]; 
}

- (void)touch:(TouchView *)touchView {
    //随机颜色
    //    [NSThread sleepForTimeInterval:0.5];
    touchView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TouchViewProDelegate
- (void)canTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     touchViewPro.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1];
}
@end
TouchViewPro.h
#import <UIKit/UIKit.h>
//协议名: 类名 + Delegate
@protocol TouchViewProDelegate <NSObject>
//协议方法
- (void)canTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
@optional
- (void)cantouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@interface TouchViewPro : UIView
@property (nonatomic, assign) id<TouchViewProDelegate> delegate;
@end

找代理的类(TouchViewPro)
1.制定协议
  a.协议名 = 类名 + Delegate;
2.写delegate属性(一旦找到代理, 和代理建立关系)
  a.属性的修饰词用assign(避免出现循环引用)
  b.属性类型用id(不确定代理是什么类型)
  c.id<协议名>(成为代理, 必须遵守协议)
3.让代理执行协议方法
TouchViewPro.m
import "TouchViewPro.h"
@implementation TouchViewPro
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_delegate respondsToSelector:@selector(canTouchesEnded:withEvent:)]) {
        [_delegate canTouchesEnded:touches withEvent:event];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_delegate respondsToSelector:@selector(cantouchesBegan:withEvent:)]) {
        [_delegate cantouchesBegan:touches withEvent:event];
    }
}
@end

'

The one who wants to wear a crown must bear the weight!
原文地址:https://www.cnblogs.com/OrangesChen/p/4925611.html