|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
CoreAnimation---制作动画很强大很喜欢的框架可以用少量的代码写出漂亮的动画CQuartz2D---强大的2D绘图库COpenGL---不用介绍了超级强大的3D库CCoreImage---准备常识
iOS处置屏幕上的触摸举措,次要触及到以下几个办法:
touchesBegan:withEvent://触摸屏幕的最入手下手被挪用
touchesMoved:withEvent://挪动过程当中被挪用
touchesEnded:withEvent://举措停止时被挪用
touchesCancelled:WithEvent:
从办法的定名能够明晰的看出该办法什么时候被挪用,最初一个对照特别。touchesCancelled:WithEvent:在CocoaTouch必需呼应延续触摸事务的体系中止时挪用。
我们只需重写这些办法,来作我们想要作的事变就能够了。
怎样完成拖动视图?
1.设置userInteractionEnabled属性为YES,同意用户交互。
2.在触摸举措入手下手时纪录肇端点。
3.在挪动过程当中,盘算以后地位坐标与肇端点的差值,即偏移量,而且挪动视图中央点至偏移量巨细的中央。
4.分离限定x坐标、与y坐标,包管用户不成将视图托出屏幕
备注:分离限定x坐标与y坐标的缘故原由是,即便向右拖动不了了,仍需包管能够向下拖动。
完成代码
以子类化UIImageView为例
[plain]
- #import<UIKit/UIKit.h>
- @interfaceGragView:UIImageView
- {
- CGPointstartPoint;
- }
- @end
#import<UIKit/UIKit.h>@interfaceGragView:UIImageView{CGPointstartPoint;}@end
[plain]
- #import"GragView.h"
- @implementationGragView
- -(id)initWithFrame:(CGRect)frame
- {
- self=[superinitWithFrame:frame];
- if(self){
- //Initializationcode
- //同意用户交互
- self.userInteractionEnabled=YES;
- }
- returnself;
- }
- -(id)initWithImage:(UIImage*)image
- {
- self=[superinitWithImage:image];
- if(self){
- //同意用户交互
- self.userInteractionEnabled=YES;
- }
- returnself;
- }
- -(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event
- {
- //保留触摸肇端点地位
- CGPointpoint=[[touchesanyObject]locationInView:self];
- startPoint=point;
- //该view置于最前
- [[selfsuperview]bringSubviewToFront:self];
- }
- -(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event
- {
- //盘算位移=以后地位-肇端地位
- CGPointpoint=[[touchesanyObject]locationInView:self];
- floatdx=point.x-startPoint.x;
- floatdy=point.y-startPoint.y;
- //盘算挪动后的view中央点
- CGPointnewcenter=CGPointMake(self.center.x+dx,self.center.y+dy);
- /*限定用户不成将视图托出屏幕*/
- floathalfx=CGRectGetMidX(self.bounds);
- //x坐标右边界
- newcenter.x=MAX(halfx,newcenter.x);
- //x坐标右侧界
- newcenter.x=MIN(self.superview.bounds.size.width-halfx,newcenter.x);
- //y坐标同理
- floathalfy=CGRectGetMidY(self.bounds);
- newcenter.y=MAX(halfy,newcenter.y);
- newcenter.y=MIN(self.superview.bounds.size.height-halfy,newcenter.y);
- //挪动view
- self.center=newcenter;
- }
- /*
- //OnlyoverridedrawRect:ifyouperformcustomdrawing.
- //Anemptyimplementationadverselyaffectsperformanceduringanimation.
- -(void)drawRect:(CGRect)rect
- {
- //Drawingcode
- }
- */
- @end
#import"GragView.h"@implementationGragView-(id)initWithFrame:(CGRect)frame{self=[superinitWithFrame:frame];if(self){//Initializationcode//同意用户交互self.userInteractionEnabled=YES;}returnself;}-(id)initWithImage:(UIImage*)image{self=[superinitWithImage:image];if(self){//同意用户交互self.userInteractionEnabled=YES;}returnself;}-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{//保留触摸肇端点地位CGPointpoint=[[touchesanyObject]locationInView:self];startPoint=point;//该view置于最前[[selfsuperview]bringSubviewToFront:self];}-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event{//盘算位移=以后地位-肇端地位CGPointpoint=[[touchesanyObject]locationInView:self];floatdx=point.x-startPoint.x;floatdy=point.y-startPoint.y;//盘算挪动后的view中央点CGPointnewcenter=CGPointMake(self.center.x+dx,self.center.y+dy);/*限定用户不成将视图托出屏幕*/floathalfx=CGRectGetMidX(self.bounds);//x坐标右边界newcenter.x=MAX(halfx,newcenter.x);//x坐标右侧界newcenter.x=MIN(self.superview.bounds.size.width-halfx,newcenter.x);//y坐标同理floathalfy=CGRectGetMidY(self.bounds);newcenter.y=MAX(halfy,newcenter.y);newcenter.y=MIN(self.superview.bounds.size.height-halfy,newcenter.y);//挪动viewself.center=newcenter;}/*//OnlyoverridedrawRect:ifyouperformcustomdrawing.//Anemptyimplementationadverselyaffectsperformanceduringanimation.-(void)drawRect:(CGRect)rect{//Drawingcode}*/@end
最终效果
<br>
我当时刚学iOS开发的时候一样的感觉总想知道原理内部怎么回事感觉在像在雾里但是iOS开发就是这样他是封闭的本身就是在雾里... |
|