IOS学习 触摸和手势UITouch 单击双击、移动视图

@interface HomeViewController : UIViewController

{

    UIView *touchView;

    UITouch *touch;

    NSInteger *tapcount;

    CGPoint point;

    UIView *movieView;

}

@end



@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    touchView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 500)];

    touchView.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:touchView];

    

    //是否支持多点触摸,默认为NO

    touchView.multipleTouchEnabled = YES ;

    

    //是否开启触摸事件,默认为开启

    touchView.userInteractionEnabled = YES;

    

    movieView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    movieView.backgroundColor = [UIColor redColor];

    [touchView addSubview:movieView];

    

}


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

//    NSLog(@"touchesBegan");

    

    touch = [touches anyObject];  //获取点击内容

    tapcount = touch.tapCount;   //获取短时间内点击的次数

    NSLog(@"tapCount = %d",tapcount);

    

    //设定单双击事件

    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];

    }

    //获取触摸点在视图上的坐标

    point = [touch locationInView:touchView];

    NSLog(@"%@",NSStringFromCGPoint(point));//将坐标转换成字符打印

}


- (void)singleTap{

    NSLog(@"单击");

}


- (void)doubleTap{

    NSLog(@"双击");

}


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

//    NSLog(@"touchesMoved");

    //重新获取

    touch = [touches anyObject];

    point = [touch locationInView:touchView];

    

    CGRect frame = movieView.frame //获取frame

    frame.origin = point //将移动光标的坐标赋值给frame

    movieView.frame = frame;  //重新赋值给视图movieView

}


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

//    NSLog(@"touchesEnded");    

}


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

//    NSLog(@"touchesCancelled");

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值