• UITouch的用法


    UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得。

    //这四个方法是UIResponder中得方法

    // Generally, all responders which do custom touch handling should override all four of these methods.

    // Your responder will receive either touchesEnded:withEvent: or touchesCancelled:withEvent: for each

    // touch it is handling (those touches it received in touchesBegan:withEvent:).

    // *** You must handle cancelled touches to ensure correct behavior in your application.  Failure to

    // do so is very likely to lead to incorrect behavior or crashes.

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

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

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

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

     

    前面写了几次提到UITouch,也提到了UITouch的三个代理方法

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

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

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

    这里还有几个代理方法

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消事件

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event;//注册接受触摸事件的Control

    下面写了一个简单的DEMO,触摸时,移动图片

    - (void)loadView {

    [super loadView];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 50.0, 45.0, 45.0)];

    image.image = [UIImage imageNamed:@"1.png"];

    image.tag = 100;

    [self.view addSubview:image];

    }

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

    {

    UITouch *touch = [touches anyObject];

    UIImageView *view1 = (UIImageView*)[self.view  viewWithTag:100];

    CGPoint point = [touch  locationInView:self.view];

    CGRect  frame = view1.frame;

    frame.origin = point;

    view1.frame = frame;

    }

    效果

    UITouch的用法UITouch的用法 

  • 相关阅读:
    HttpServer发送数据到kafka
    Leetcode[33]-Search in Rotated Sorted Array
    站在淘宝天猫两大平台背后的平台
    JSON格式的时间“/Date(1530104033000)/”格式转为正常的年-月-日 格式的代码
    火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)
    echart 图例设置自定义图标?
    easyui datagrid实现拖动表头
    浮动闭合最佳方案:clearfix
    ASP.NET MVC提交LIST列表到后台接收不到数据
    mysql常用命令
  • 原文地址:https://www.cnblogs.com/worldtraveler/p/4597626.html
Copyright © 2020-2023  润新知