touchesMoved 实现拖拽

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

{

 

     UITouch *touch = [touches anyObject];// 某一个手指

    CGPoint currentPoint = [touch locationInView:self.superview]; // 自己相对于父视图的坐标

    CGPoint previousPoint = [touch previousLocationInView:self.superview];

    

    CGFloat dltX = currentPoint.x - previousPoint.x;

    CGFloat dltY = currentPoint.y - previousPoint.y;

    CGPoint center = self.center;    

    CGPoint newCenter = CGPointMake(center.x + dltX, center.y + dltY);    

    self.center = newCenter;

    

     self.backgroundColor = [UIColorcolorWithRed:(arc4random() % 256)/255.0green:(arc4random() % 256)/255.0blue:(arc4random() % 256)/255.0alpha:1.0];

}

 

posted @ 2014-04-17 09:01  忆轩  阅读(1007)  评论(0)    收藏  举报