zoukankan      html  css  js  c++  java
  • UI2_UIGesture

    //
    //  ViewController.h
    //  UI2_UIGesture
    //
    //  Created by zhangxueming on 15/7/9.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIGestureRecognizerDelegate>
    
    
    @end
    
    //
    //  ViewController.m
    //  UI2_UIGesture
    //
    //  Created by zhangxueming on 15/7/9.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSArray *names = @[@"blue",@"red",@"yellow"];
        for (int i=0; i<3; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100*i, 180+100*i, 100, 100)];
            imageView.image = [UIImage imageNamed:names[i]];
            [self.view addSubview:imageView];
            //打开用户交互使能
            imageView.userInteractionEnabled = YES;
            
            //添加点击手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
            //设置点击次数
            tap.numberOfTapsRequired = 1;
            //设置手指个数
            tap.numberOfTouchesRequired = 2;
            //给imageView 添加手势
            [imageView addGestureRecognizer:tap];
            
            //添加长按手势
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
            longPress.numberOfTapsRequired = 0;
            //longPress.numberOfTouchesRequired = 2;
            [imageView addGestureRecognizer:longPress];
            
            //添加移动手势
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer  alloc] initWithTarget:self action:@selector(panGesture:)];
            [imageView addGestureRecognizer:pan];
            
            //捏合手势
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
            [imageView addGestureRecognizer:pinch];
            //旋转手势
            UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
            [imageView addGestureRecognizer:rotation];
            //轻扫手势
            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
            [imageView addGestureRecognizer:swipe];
        }
    }
    
    - (void)tapGesture:(UITapGestureRecognizer *)tap
    {
        NSLog(@"图片被点击");
    }
    
    - (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
    {
        NSLog(@"长按手势被触发");
    }
    
    - (void)panGesture:(UIPanGestureRecognizer *)pan
    {
        NSLog(@"移动手势被触发");//5 10 15   5 5 5
        UIView *view = pan.view;//5+5+5
        //获取手势的偏移量
        CGPoint px = [pan translationInView:self.view];
        if(pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged)
        {
        //改变手势对应的view中心点坐标
            pan.view.center = CGPointMake(view.center.x+px.x, view.center.y+px.y);
        }
        //设置偏移量为0;
        [pan setTranslation:CGPointZero inView:self.view];
    }
    
    - (void)pinchGesture:(UIPinchGestureRecognizer *)pinch
    {
        NSLog(@"捏合手势被触发");
        if(pinch.scale == UIGestureRecognizerStateBegan || pinch.scale ==UIGestureRecognizerStateChanged)
        {
            pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
        }
        //设置系数为1
        pinch.scale = 1.0;
    }
    
    - (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
    {
        NSLog(@"旋转手势被触发");
        if (rotation.state == UIGestureRecognizerStateBegan || rotation.state == UIGestureRecognizerStateChanged) {
            rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
        }
        
        rotation.rotation = 0.0;
    }
    
    - (void)swipeGesture:(UISwipeGestureRecognizer *)swipe
    {
        NSLog(@"轻扫手势被触发");
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
  • 相关阅读:
    设计算法,根据输入的学生人数和成绩建立一个单链表,并累计成绩不及格的人数。
    git的使用,看这一篇就够啦!(包含github、码云、gitlab)
    设单链表的数据为互不相等的整数,建立一个单链表,并设计一个算法,找出单链表中元素值最大 的结点。
    “Failed to get convolution algorithm. This is probably because cuDNN failed to initialize”错误的解决办法
    回文指的是一个字符串从前面读和从后面读都一 样,编写一个算法判断一个字符串是否为回文。
    怎么搭建一个5T的私有云盘
    基于大数据分析与可视化的疫情信息发布系统
    如何给oneindex网盘增加评论、密码查看、read me,头提示功能。
    解析原理:微信自动查找优惠券做返利机器人是怎么实现的
    【Swift】接入阿里云一键登录(源码,可以直接贴走)
  • 原文地址:https://www.cnblogs.com/0515offer/p/4638914.html
Copyright © 2011-2022 走看看