小功能--视图跟着触点移动

手指点击视图,移动手指,视图跟着手指进行移动

实现方法,可以进行自定义视图,在视图中添加方法;

#import "DargView.h"
@interface DargView ()
{
    CGPoint point1;
}
@end
@implementation DargView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"开始");
    self.backgroundColor = [UIColor yellowColor];
    //1.
    //获取手指位置(自身的坐标系)
    UITouch *touch = [touches anyObject];
    //locationInView:手指触摸哪个视图上的坐标 后面写self说明他依据的是他所在的坐标系
    CGPoint point = [touch locationInView:self.superview];//将坐标系转换成父试图的坐标系
    point1.x = point.x - self.center.x;
    point1.y = point.y - self.center.y;
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //手指移动 视图跟着移动
    NSLog(@"移动");
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.superview];//将坐标系转换成父试图的坐标系
    CGPoint pointEmpty = CGPointMake(point.x - point1.x,point.y - point1.y);
    self.center = pointEmpty;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"结束");
     self.backgroundColor = [UIColor redColor];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"....");
}

这只是一个简单的小玩意儿~~~~

posted @ 2017-02-04 11:45  NSJELLY  阅读(396)  评论(0编辑  收藏  举报