iOS touch事件的多点触控案例和获取touch的属性

#import "ViewController.h"


@interface ViewController ()

@property (nonatomic,strong) NSArray *images;

@end


@implementation ViewController



//懒加载的方式

- (NSArray *)images

{

    if (_images ==nil)

    {

        _images = @[

                    [UIImage imageNamed:@"spark_blue"],

                    [UIImage imageNamed:@"spark_cyan"]

                    ];

    }

    return_images;

}


//手指按下

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    int i = 0;

    //通过forin循环获取触摸对象

    for (UITouch * touchin touches)

    {

        //获取位置

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

        

        //创建图片框

        

        UIImageView * imageView = [[UIImageViewalloc] initWithImage:self.images[i]];

        

        imageView.center = locP;

        

        [self.viewaddSubview:imageView];

        

        //通过动画的方式 1s后让它消失

        [UIViewanimateWithDuration:1.5animations:^{

            imageView.alpha = 0;

        } completion:^(BOOL finished) {

            [imageView removeFromSuperview];

        }];

        

        i++;

        

    }

}


//手指移动

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    int i = 0;

    //通过forin循环获取触摸对象

    for (UITouch * touchin touches)

    {

        //获取位置

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

        

        //创建图片框

        

        UIImageView * imageView = [[UIImageViewalloc] initWithImage:self.images[i]];

        

        imageView.center = locP;

        

        [self.viewaddSubview:imageView];

        

        //通过动画的方式 1s后让它消失

        [UIViewanimateWithDuration:1.5animations:^{

            imageView.alpha = 0;

        } completion:^(BOOL finished) {

            [imageView removeFromSuperview];

        }];

        

        i++;

        

    }

}

//手指抬起

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

{

    NSLog(@"%@",@(self.view.subviews.count));

}

========================================================


    //获取触摸对象

    UITouch * touch = touches.anyObject;

    

    //获取点击次数

    //    NSLog(@"%@",@(touch.tapCount));

    //获取触摸的view

    //    NSLog(@"%@",touch.view);

    

    //获取上一个点

    CGPoint preP = [touch previousLocationInView:touch.view];

    

    //获取当前点

    CGPoint locP = [touch locationInView:touch.view];

    

    NSLog(@"pre---%@",NSStringFromCGPoint(preP));

    

    NSLog(@"curren-----%@",NSStringFromCGPoint(locP));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值