鼠标拖动视图

MyView.h中定义两个属性, 用来接收移动前的点坐标, 和移动后的点坐标.


@property(nonatomic, assign) CGPoint startPoint;


@property(nonatomic, assign) CGPoint endPoint;



MyView.m: 


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

{


//用self.startPoint存放点击开始时的点坐标.

    UITouch *touch = [touches anyObject];

    

    self.startPoint = [touch locationInView:self];


    

}




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

{

//用self.endPoint存放移动后的点坐标.

    UITouch *touch = [touches anyObject];

    

    self.endPoint = [touch locationInView:self];

    

//思想: 鼠标移动多少, 图片的中心(center)也移动多少  

//鼠标移动的水平方向距离: 移动后的点坐标的横坐标(_endPoint.x) - 开始触摸时的点坐标的横坐标(_startPoint.x)

//鼠标移动的竖直方向距离: 移动后的点坐标的横坐标(_endPoint.y) - 开始触摸时的点坐标的横坐标(_startPoint.y)


    CGFloat x = self.center.x + _endPoint.x - _startPoint.x;

    

    CGFloat y = self.center.y + _endPoint.y - _startPoint.y;

    

    self.center = CGPointMake(x, y);

    

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值