进击的UI---------------target/action设计模式&Delegate&手势识别

1.target/action设计模式:
AppDelegate.m
RootViewController.m
 
ClickView.h
ClickView.m
ColorView.h
ColorView.m
RootView.h
RootView.m
ButtonView.h
ButtonView.m
 
2.delegate设计模式(代理):
AppDelegate.m
RootViewController.m
RootView.h:
RootView.m:
colorView.h
colorView.m
clickView.h
clickView.m
3.UIImageView:
self.backgroundColor = [UIColor colorWithRed:arc4random()%2/1.0 green:arc4random()%2/1.0 blue:arc4random()%2/1.0 alpha:1];
    self.mv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
    self.mv.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:_mv];
4.手势识别器(Gesture Recognizor)
1⃣️:手势识别器有7个子类 分别是识别轻拍手势,平移手势,轻扫手势,缩放手势,旋转手势,长按手势,以及屏幕边界平移手势,一旦指定的手势被识别,就可以执行自己定义好的操作;
2⃣️:代码
// 轻拍/点击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];// 手势添加到相应的控件上添加手势
    tap.numberOfTapsRequired = 1;// 连续点击1下,点击数
//    tap.numberOfTouchesRequired = 2;// 手指数(alt + 点击)
    self.fv.mv.userInteractionEnabled = YES;
    [self.fv.mv addGestureRecognizer:tap];
    _flag = YES;
    // 长按
    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressAction:)];
    [self.fv.mv addGestureRecognizer:press];
    press.minimumPressDuration = 1;  // 最短长按时间
    // 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self.fv.mv addGestureRecognizer:pinch];
// 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [self.fv.mv addGestureRecognizer:rotation];
// 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.fv.mv addGestureRecognizer:pan];
// 轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    [self.fv.mv addGestureRecognizer:swipe];
// 屏幕边缘轻扫
    UIScreenEdgePanGestureRecognizer *sep = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(sepAction:)];
    [self.fv.mv addGestureRecognizer:sep];
    //用户交互打开;UIImageView的用户交互默认是关闭的,需要打开才能正常使用;
//    UITapGestureRecognizer                      是轻拍⼿手势识别器,能识别轻拍操作.
//    UILongPressGestureRecognizer                长按⼿手势识别器,能识别⻓长按操作.
//    UIRotationGestureRecognizer                   旋转⼿手势识别器,能识别旋转操作.
//    UIPinchGestureRecognizer                      捏合⼿手势识别器,能识别捏合操作.
//    UIPanGestureRecognizer                        平移⼿手势识别器,能识别拖拽操作.
//    UISwipeGestureRecognizer                      轻扫⼿手势识别器,能识别拖拽操作.
//    UIScreenEdgePanGestureRecognizer        屏幕边缘轻扫识别器,ios7中新增的⼿手势.
}
- (void)tapAction:(UITapGestureRecognizer *)sender{
    NSLog(@"轻拍点我啊");
    UIImageView *temp = (UIImageView *)sender.view;
    temp.image = [UIImage imageNamed:@"3.jpg"];
//    if (self.flag == YES) {
//        self.fv.mv.image = [UIImage imageNamed:@"2.jpg"];
//        _flag = NO;
//    }else{
//        self.fv.mv.image = [UIImage imageNamed:@"1.jpg"];
//        _flag = YES;
//    }
}
- (void)pressAction:(UILongPressGestureRecognizer * )sender{
    NSLog(@"长按我啊!!");
    if (sender.state == UIGestureRecognizerStateBegan) {
        [UIView animateWithDuration:1 animations:^{
            CGRect temp = sender.view.frame;
        temp.size.width += 50;
        temp.size.height += 50;
        sender.view.frame = temp;
        }];
       
    }else if (sender.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:1 animations:^{
            CGRect temp = sender.view.frame;
        temp.size.width -= 50;
        temp.size.height -= 50;
        sender.view.frame = temp;
        }]; 
    }
}
- (void)pinchAction:(UIPinchGestureRecognizer *)sender{
    NSLog(@"你捏我啊!!");
    sender.view.transform = CGAffineTransformScale(sender.view.transform,sender.scale, sender.scale);
}
- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
    NSLog(@"你旋转我啊!!");
}
- (void)panAction:(UIPanGestureRecognizer *)sender{
    NSLog(@"你平移我啊!!");
}
- (void)swipeAction:(UISwipeGestureRecognizer *)sender{
    NSLog(@"你轻扫我啊!!");
}
- (void)sepAction:(UIScreenEdgePanGestureRecognizer *)sender{
    NSLog(@"你屏幕边缘轻扫我啊!!");
}
原文地址:https://www.cnblogs.com/sharkHZ/p/4984131.html