当前位置: 首页 > 工具软件 > YDUI-Touch > 使用案例 >

UI----Touch

文英达
2023-12-01

UI—-Touch

触摸开始时调用的方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.viewL.touchView];
    CGPoint point2 = [touch previousLocationInView:self.viewL.touchView];
    NSLog(@"%.2f %.2f",point2.x,point2.y);
    NSLog(@"%.2f %.2f",point.x,point.y);
}

触摸结束时调用的方法

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

触摸移动时调用的方法

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point1 = [touch locationInView:self.viewL.touchView];
    CGPoint point2 = [touch previousLocationInView:self.viewL.touchView];
//    NSLog(@"point1 : %.2f %.2f",point1.x,point1.y);
//    NSLog(@"point2 : %.2f %.2f",point2.x,point2.y);
    CGPoint center = self.viewL.touchView.center;
    CGFloat x = point1.x - point2.x;
    CGFloat y = point1.y - point2.x;
    center.x += x;
    center.y += y;
    //保持在屏幕内移动
//    if (center.x < 100 ) {
//        center.x = 100;
//    }else if (center.x > CGRectGetMaxX(self.viewL.frame) - 100){
//        center.x = CGRectGetMaxX(self.viewL.frame) - 100;
//    }
//    if (center.y < 100) {
//        center.y = 100;
//    }else if (center.y > CGRectGetMaxY(self.viewL.frame) - 100){
//        center.y = CGRectGetMaxY(self.viewL.frame) - 100;
//    }
    self.viewL.touchView.center = CGPointMake(center.x, center.y);

触摸中断取消是调用的方法

//
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"cancel %s",__FUNCTION__);
}
 类似资料: