首页 > 移动平台 > 详细

创建一个可移动的视图,让视图随着鼠标的移动而移动

时间:2016-02-01 17:52:52      阅读:239      评论:0      收藏:0      [点我收藏+]

1.首先自己定义视图,继承自UIView

MoveView.h

@interface MoveView :UIView

@property(nonatomic,assign)CGPoint beginPoint;

//记录点击的最初位置

@end



MoveView.m

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

{

    UITouch *touch=[touches anyObject];

    //记录下触摸的最初位置。相对于自身

    _beginPoint=[touch locationInView:self];


}


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

{

    UITouch *touch=[touches anyObject];

//记录移动到的位置,此时此时相对于父视图

    CGPoint endPoint=[touch locationInView:self.superview];

    [self setFrame:CGRectMake(endPoint.x-_beginPoint.x

                              , endPoint.y-_beginPoint.y,self.frame.size.width,self.frame.size.height)];

}

2.查UIView的Api可知,UIView继承自UIResponder,在UIResponder中有以下四个方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//触摸開始

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//触摸移动

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//触摸结束

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消(主要用于突发事件,此次触摸失效,比如来电话了,电话界面弹出。用户此次触摸被取消)

能够依据这几个方法对一些触摸事件进行处理。


创建一个可移动的视图,让视图随着鼠标的移动而移动

原文:http://www.cnblogs.com/bhlsheji/p/5175394.html

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