仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1616|回复: 19
打印 上一主题 下一主题

[学习教程] IOS教程之iOS开辟进门之――拖动视图仓酷云

[复制链接]
爱飞 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:44:48 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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开发就是这样他是封闭的本身就是在雾里...
爱飞 该用户已被删除
沙发
 楼主| 发表于 2015-4-22 04:14:18 | 显示全部楼层
开始的时候甚至想放弃,不过想想自己的未来,只能咬牙坚持,课下就不停的缠着老师。放学就补基础,这些基础的东西没有速成的,只有刻苦努力。我是后来发现的,转变自己的心态,不要读书看资料当成一种痛苦
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-16 04:07

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表