你的位置:首页 > 操作系统

[操作系统]九:UIView的拖拽


  做出来效果:屏幕上的一块UIView,点击后能自由移动

 1 #import "GreenView.h" 2 @implementation GreenView 3 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 4 { 5   UITouch *touch = [touches anyObject]; 6   CGPoint currentP = [touch locationInView:self]; 7   CGPoint previousP = [touch previousLocationInView:self]; 8   CGFloat offsetX = currentP.x - previousP.x; 9   CGFloat offsetY = currentP.y - previousP.y;10   self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);11 }12 @end