1.
2.
//
// RedView.m
#import "RedView.h"
@implementation RedView
/**
* 当手指在View上移动就会调用
*/
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 取出UITouch对象
UITouch *touch = [touches anyObject];
// 获取当前触摸点
CGPoint currentP = [touch locationInView:self];
// 获取上一个触摸点
CGPoint previousP = [touch previousLocationInView:self];
// 获取偏移量
CGFloat offsetX = currentP.x - previousP.x;
CGFloat offsetY = currentP.y - previousP.y;
// 修改View的位置(frame\center\transform)
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}
@end
演示效果: