ios 触摸与手势 UItouch

#import <UIKit/UIKit.h>

@interface TouchView : UIView {
    UIView *movieView;
}
@end
#import "TouchView.h"

@implementation TouchView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //默认触摸是单点触摸,
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = YES;
        movieView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        movieView.backgroundColor = [UIColor blueColor];
        [self addSubview:movieView];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"touchesBegan");
    //判断是单击还是双击
    UITouch *touch = [touches anyObject];
    NSUInteger tapCount = touch.tapCount;
    NSLog(@"%d", tapCount);
//    //双击,也会调用单机
//    if (tapCount == 1) {
//        [self singleTap];
//    }else if (tapCount == 2){
//        [self doubleTap];
//    }
    
    //取消双击时候的单击事件
    if (tapCount == 1) {
        [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
    }else if (tapCount == 2){
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
        [self doubleTap];
    }

    //获取触摸的点的坐标
    CGPoint point = [touch locationInView:self];
    NSLog(@"%@", NSStringFromCGPoint(point));
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"touchesMoved");
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGRect frame = movieView.frame;
    frame.origin = point;
    movieView.frame = frame;
}
//当触摸被电话等事件取消
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"touchesCancelled");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"touchesEnded");
}

- (void)singleTap
{
    NSLog(@"单击");
}

- (void)doubleTap
{
    NSLog(@"双击");
}

- (void)dealloc
{
    [movieView release];
    [super dealloc];
}

@end





#import <UIKit/UIKit.h>

@interface TouchPinch : UIView {
    double lastValue;
}
@end
#import "TouchPinch.h"

@implementation TouchPinch

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor grayColor];
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = YES;
    }
    return self;
}

//touch的事件传递机制
//点击设备-->(ios 捕获一系列的触摸,封装成UIEvent )App-->window-->Gesture recognizer--> View
//事件响应处理
//消息传递给视图控制器,如果没有控制器,则传递给父视图,如果不处理该消息,则继续往上传递,如果最上层的控制器也不处理,
//则把事件交给window对象,如果window对象不处理,则交给UIapplication实例,如果不处理,则丢弃事件。

//事件捕获后分发:由UIApplication分发给window,由其发给最上层触摸的视图,。。。。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //手指捏合
    //判断是否有两个点同时在屏幕
    if ([touches count] == 2){
        NSArray *touchArray = [touches allObjects];
        UITouch *firstTouch = [touchArray objectAtIndex:0];
        UITouch *secondTouch = [touchArray objectAtIndex:1];
        
        //判断两个点之间的距离,然后做相应的操作
        CGPoint point1 = [firstTouch locationInView:self];
        CGPoint point2 = [secondTouch locationInView:self];
        
        double distan = [self distance:point1 point:point2];
        NSLog(@"%f", distan);
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //手指捏合
    //判断是否有两个点同时在屏幕
    if ([touches count] == 2){
        NSArray *touchArray = [touches allObjects];
        UITouch *firstTouch = [touchArray objectAtIndex:0];
        UITouch *secondTouch = [touchArray objectAtIndex:1];
        
        //判断两个点之间的距离,然后做相应的操作
        CGPoint point1 = [firstTouch locationInView:self];
        CGPoint point2 = [secondTouch locationInView:self];
        
        double distan = [self distance:point1 point:point2];
        float subValue = distan - lastValue;
        if (subValue < 0){
            NSLog(@"缩小");
        }else{
            NSLog(@"放大");
        }
        lastValue = distan;
        NSLog(@"%f", distan);
    }
}

//计算两个点之间距离
- (double)distance:(CGPoint)point1 point:(CGPoint)point2
{
    return sqrt(pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2));
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值