首页 > 移动平台 > 详细

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

时间:2017-02-04 14:29:44      阅读:155      评论:0      收藏:0      [点我收藏+]

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

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

#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(@"....");
}

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

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

原文:http://www.cnblogs.com/nsjelly/p/6364556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!