• UI:触摸事件 与 事件的回应


    事件分类:晃动、触摸、远程控制(如遥控器、红外控制)

    触摸开始时候的方法(判断单击,双击,三击事件可以写在这里)

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

    //触摸开始时候的方法 ()
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"触摸开始");
        //点击的时候随机改变背景颜色
    //    [self changeSelColor];
        //touches 存储触摸屏幕时的手指对象
        //UITouch 表示是一个手指类,每一个手指对象都是一个 UITouch 类型的对象
        //1.取出手指对象
        UITouch * aTouch = [touches anyObject];
        //2.根据点击的次数来进行相应的处理
        if (aTouch.tapCount == 0) {
            NSLog(@"0");//这里的永远不会打印出来,因为,触摸必定会点击到屏幕
        }
        if ( aTouch.tapCount == 1) {
    //        [self changeSelColor];
            //让单击操作延迟。看是否有双击事件操作
            [self performSelector:@selector(changeSelColor) withObject:nil afterDelay:0.3];
        }
        if (aTouch.tapCount == 2) {
            [self changFatherColor];
            //双击操作的时候,取消改变自身颜色的操作
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelColor) object:nil];
        }
        if (aTouch.tapCount == 3) {
            [self changSelfStation];//点击3次,改变自身的位置
        }
    }
    View Code   我们的双击,三击,单击事件 demo

    手指还没有离开屏幕时候(在屏幕上移动手指)触发

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}

    触发结束时候,手指离开屏幕的那一时刻触发

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

    触摸中断时候要触发的方法

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}

    随机改变颜色(一般写在一个方法的分类里,方便调用)

    [UIColor  colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

     

    UIEvent:事件,是由硬件捕捉的⼀一个表⽰示⽤用户操作设备的对象。 (触摸事件、晃动事件、远程控制事件)

    触摸事件:⽤用户通过触摸设备屏幕操作对象、输⼊入数据。⽀支持多点 触摸,包含1个到多个触摸点 

    UIView⽀支持触摸事件(因为继承于UIResponder),⽽而且⽀支持多 点触摸。 

    我们视图上的Button 我们的程序怎么知道我们点击了?每一个程序都是一个 UIApplication 对象

        yellowView.userInteractionEnabled = NO;黄色视图的用户交互属性被关闭。

    当我们查找的时候,是一层一层去查找,当我们去响应的时候,是从响应的地方开始,往上提交

     

    代码demo


    //
    //  AppDelegate.m
    
    #import "AppDelegate.h"
    
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        RootViewController * rootView = [[RootViewController alloc]init];
        self.window.rootViewController = rootView;
        [rootView release];
        
    
        
        return YES;
    }
    -(void)dealloc{
        [self release];
        [super dealloc];
    }
    View Code mian.m
    #import <UIKit/UIKit.h> @interface PinchView : UIView @end
    View Code PinchView.h
    //
    //  PinchView.m
    
    #import "PinchView.h"
    
    //捏合效果(在 touchesMoved 里面实现)
    @implementation PinchView
    //IOS 支出多点触控,只不过默认的是开启单点触控
    //重写初始化方法,修改为支持多点触控
    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.multipleTouchEnabled = YES;//支持多点触摸
        }
        return self;
    }
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        //一旦发现是用一个手指在操作的话,则不进行下面的捏合操作
        if (touches.count == 1) {
            return ;//结束下面的代码 (return 后面的代码永远不会被编译到)
        }
        //1.获取触摸屏的两个手指对象
        NSArray * allTouches = [touches allObjects];
        UITouch * firstTouch = [allTouches firstObject];//取出第一个手指对象
        UITouch * secondTouch = [allTouches lastObject];//取出第二个手指对象
        //2.获取捏合后手指的位置
        CGPoint currentPoint1 = [firstTouch locationInView:self];
        CGPoint currentPoint2 = [secondTouch locationInView:self];
        //3.获取手指捏合后的距离
        CGFloat currentDistance = [self destanceBetweenPoint1:currentPoint1 Ponit2:currentPoint2];
        //4.获取捏合之前的手指的位置
        CGPoint previousPoint1 = [firstTouch previousLocationInView:self];
        CGPoint previousPoint2 = [secondTouch previousLocationInView:self];
        //5.获取捏合之前的两手指之间的距离
        CGFloat previousDistance = [self destanceBetweenPoint1:previousPoint1 Ponit2:previousPoint2];
        //6.缩放比 (捏合后和捏合前的比例)
        CGFloat scale  = currentDistance/previousDistance;
        //7.修改视图的大小 修改 bounds 保持中心点不变
        self.bounds = CGRectMake(0, 0, self.frame.size.width*scale, self.frame.size.height*scale);
        
        
        
        /*
         CGFloat ph1 = currentPoint1.y - currentPoint2.y;
        //捏合前的两手指位置
        CGPoint previousLocation1 = [firstTouch previousLocationInView:self];
        CGPoint previousLocation2 = [secondTouch previousLocationInView:self];
        //捏合前的两手指距离
        CGFloat ph2 = previousLocation1.y - previousLocation2.y;
        //计算比例
        CGFloat change = ph1/ph2;
        //得到缩放后的效果
        self.center = self.center;
        CGFloat w = self.frame.size.width * change;
        CGFloat h = self.frame.size.height * change;
        */
    
    }
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
        
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        
    }
    
    //利用勾股定理 计算两个点之间的距离
    -(CGFloat)destanceBetweenPoint1:(CGPoint )point1 Ponit2:(CGPoint )point2{
        CGFloat dx = point1.x - point2.x;
        CGFloat dy = point1.y - point2.y;
        return sqrt(dx*dx + dy*dy);
    }
    @end
    View Code PinchView.m
    //
    //  TouchView.h
    
    #import <UIKit/UIKit.h>
    #import "UIColor+Addition.h"
    #import "PanView.h"
    
    @interface TouchView : UIView
    
    -(void)changeSelColor;
    -(void)changFatherColor;
    -(void)changSelfStation;
    @end
    View Code TouchView.h
    //
    //  TouchView.m
    
    #import "TouchView.h"
    
    @implementation TouchView
    
    //对于 UIView 类的视图可以接受触摸事件,但是让他响应,必须实现以下的几个方法
    
    //单击的时候改变自己的颜色,双击的时候改变父视图的颜色
    
    
    
    //触摸开始时候的方法 ()
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"触摸开始");
        //点击的时候随机改变背景颜色
    //    [self changeSelColor];
        //touches 存储触摸屏幕时的手指对象
        //UITouch 表示是一个手指类,每一个手指对象都是一个 UITouch 类型的对象
        //1.取出手指对象
        UITouch * aTouch = [touches anyObject];
        //2.根据点击的次数来进行相应的处理
        if (aTouch.tapCount == 0) {
            NSLog(@"0");//这里的永远不会打印出来,因为,触摸必定会点击到屏幕
        }
        if ( aTouch.tapCount == 1) {
    //        [self changeSelColor];
            //让单击操作延迟。看是否有双击事件操作
            [self performSelector:@selector(changeSelColor) withObject:nil afterDelay:0.3];
        }
        if (aTouch.tapCount == 2) {
            [self changFatherColor];
            //双击操作的时候,取消改变自身颜色的操作
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelColor) object:nil];
        }
        if (aTouch.tapCount == 3) {
            [self changSelfStation];//点击3次,改变自身的位置
        }
    }
    //手指还没有离开屏幕  (手指移动的时候)
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        //手指移动的时候,改变父视图的颜色
        [self changFatherColor];
    }
    //触摸中断的时候触发的事件 (例如,手机有电话了)
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
        self.backgroundColor = [UIColor blackColor];
    }
    //触摸结束的时候触发的事件 (手指离开屏幕的时候)
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        //x 在 100-300    y 在 100 - 500
    //    [self changSelfStation];
    }
    
    //改变自身视图的颜色
    -(void)changeSelColor{
        self.backgroundColor = [UIColor randomColor];
    }
    //改变父视图的颜色
    -(void)changFatherColor{
        self.superview.backgroundColor = [UIColor randomColor];
    }
    //改变自身的位置
    -(void)changSelfStation{
        self.center = CGPointMake(arc4random()%((300-100+1)+100)*1.0, arc4random()%((500-100+1)+100)*1.0);
    }
    
    
    
    @end
    View Code TouchView.m
    #import <UIKit/UIKit.h> @interface PanView : UIView @end
    View Code PanView.h 
    #import "PanView.h"
    
    @implementation PanView
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        //1.获取触摸屏幕的手指对象
        UITouch * aTouch = [touches anyObject];
        //2.获取手指所在点移动之前的位置和移动之后的位置
        CGPoint currentLocation = [aTouch locationInView:self];//当前点位置,移动之后
        CGPoint previousLocation = [aTouch previousLocationInView:self];//原来点位置,移动之前的点位置
        //3.计算改变量
        CGFloat dx = currentLocation.x - previousLocation.x;
        CGFloat dy = currentLocation.y - previousLocation.y;
        //4.移动之后视图所在的位置(中心点)
        self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
    
    }
    @end
    View Code PanView.m
    #import <UIKit/UIKit.h>
    
    @interface UIColor (Addition)
    //声明类方法的随机色
    +(UIColor * )randomColor;
    @end
    View Code UIColor+Addition.h
    #import "UIColor+Addition.h"
    
    @implementation UIColor (Addition)
    
    +(UIColor * )randomColor{
    //    return [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
        return [self colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    }
    
    @end
    View Code
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    View Code RootViewController.h
    //
    //  RootViewController.m
    
    #import "RootViewController.h"
    #import "TouchView.h"
    #import "PanView.h"
    #import "PinchView.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //视图类都可以接触到触摸的事件
        self.view.backgroundColor = [UIColor grayColor];
        TouchView * redView = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        redView.backgroundColor = [UIColor redColor];
        [self.view addSubview:redView];
        [redView release];
        
        PanView * yellowView = [[PanView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
        yellowView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:yellowView];
        [yellowView release];
        
        PinchView * greenView = [[PinchView alloc]initWithFrame:CGRectMake(20, 50, 300, 400)];
        greenView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:greenView];
        [greenView release];
        
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    View Code
  • 相关阅读:
    uva11922splay
    获取的二维数组排序
    二维数组排序
    $.extend
    <eq>标签
    datagrid时间插件
    id=%d是什么意思呢?
    获得某一月的第一天,最后一天
    数组合并
    phpexcel 导入导出excel表格
  • 原文地址:https://www.cnblogs.com/benpaobadaniu/p/4774792.html
Copyright © 2020-2023  润新知