您的位置:首页 > 其它

利用touches方法拖动视图移动

2015-11-21 11:26 281 查看
拖动视图

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

NSLog(@"摸我了");

self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1];



//获得当前触摸的点,touches内只有一个触摸对象

UITouch *touch = [touches anyObject];



CGPoint point = [touch locationInView:self];

NSLog(@"%f, %f", point.x,point.y);

self.firstPoint = point;

}

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

NSLog(@"移动了");

//获得当前触摸的点,touches内只有一个触摸对象

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];

NSLog(@"%f, %f", point.x,point.y);

self.center = CGPointMake(self.center.x + point.x - self.firstPoint.x , self.center.y + point.y - self.firstPoint.y);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: