UITouch

ViewController-UITouch

//触摸开始
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];

//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];

NSLog(@"point == %@",NSStringFromCGPoint(point));

}

//触摸移动
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];

//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];

NSLog(@"point == %@",NSStringFromCGPoint(point));

}

//触摸结束
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];

//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];

NSLog(@"point == %@",NSStringFromCGPoint(point));

}

//触摸取消
- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];

//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];

NSLog(@"point == %@",NSStringFromCGPoint(point));

}

左右滑动切换图片练习

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imgView.image = [UIImage imageNamed:@”1.jpg”];
    [self.view addSubview:_imgView];

    _count = 1;
    }

  • (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
    {
    UITouch *touch = [touches anyObject];
    _beiginPoint = [touch locationInView:self.view];
    }

  • (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
    {
    UITouch *touch = [touches anyObject];
    _endPoint = [touch locationInView:self.view];

    [self compare];
    }

  • (void)compare
    {
    //右滑
    if (_beiginPoint.x - _endPoint.x > 0) {
    _count ++;
    if (_count == 5) {
    _count = 1;
    }
    _imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@”%ld.jpg”,_count]];
    }

    //左滑
    else if(_beiginPoint.x - _endPoint.x < 0)
    {
    _count –;
    if (_count == 0) {
    _count = 4;
    }
    _imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@”%ld.jpg”,_count]];
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值