当前位置: 移动技术网 > IT编程>移动开发>IOS > 举例讲解iOS开发中拖动视图的实现

举例讲解iOS开发中拖动视图的实现

2019年07月24日  | 移动技术网IT编程  | 我要评论

长沙不孕不育医院,李雨桐回应薛之谦,天使之争男主角

预备知识
ios处理屏幕上的触摸动作,主要涉及到以下几个方法:

复制代码 代码如下:

touchesbegan:withevent:          //触摸屏幕的最开始被调用
touchesmoved:withevent:         //移动过程中被调用
touchesended:withevent:         //动作结束时被调用
touchescancelled:withevent:


从方法的命名可以清晰的看出该方法何时被调用,最后一个比较特殊。touchescancelled:withevent:在cocoa touch必须响应持续触摸事件的系统中断时调用。
我们只要重写这些方法,来作我们想要作的事情就可以了。

如何实现拖动视图?
1.设置userinteractionenabled属性为yes,允许用户交互。
2.在触摸动作开始时记录起始点。
3.在移动过程中,计算当前位置坐标与起始点的差值,即偏移量,并且移动视图中心点至偏移量大小的地方。
4.分别限制x坐标、与y坐标,保证用户不可将视图托出屏幕

备注:分别限制x坐标与y坐标的原因是,即使向右拖动不了了,仍需保证可以向下拖动。

其实,功能比较简单,就是ios手势动画中的拖动。来看一下基本的写法:

1.注册拖动动画

复制代码 代码如下:

uipangesturerecognizer * pangesturerecognizer = [[uipangesturerecognizer alloc] initwithtarget:self
                                                                                            action:@selector(dohandlepanaction:)];
    [self.vlight addgesturerecognizer:pangesturerecognizer];


注:vlight就是要加入拖动的view子类。

2.拖动处理函数

复制代码 代码如下:

- (void) dohandlepanaction:(uipangesturerecognizer *)paramsender{
   
    cgpoint point = [paramsender translationinview:self.view];
    nslog(@"x:%f;y:%f",point.x,point.y);
   
    paramsender.view.center = cgpointmake(paramsender.view.center.x + point.x, paramsender.view.center.y + point.y);
    [paramsender settranslation:cgpointmake(0, 0) inview:self.view];
   

}


实现代码
以子类化uiimageview为例

复制代码 代码如下:

#import <uikit/uikit.h> 
 
@interface gragview : uiimageview 

    cgpoint startpoint; 

@end 

#import "gragview.h" 
 
@implementation gragview 
 
- (id)initwithframe:(cgrect)frame 

    self = [super initwithframe:frame]; 
    if (self) { 
        // initialization code 
        //允许用户交互 
        self.userinteractionenabled = yes; 
    } 
    return self; 

 
- (id)initwithimage:(uiimage *)image 

    self = [super initwithimage:image]; 
    if (self) { 
        //允许用户交互 
        self.userinteractionenabled = yes; 
    } 
    return self; 

 
- (void) touchesbegan:(nsset *)touches withevent:(uievent *)event 

    //保存触摸起始点位置 
    cgpoint point = [[touches anyobject] locationinview:self]; 
    startpoint = point; 
     
    //该view置于最前 
    [[self superview] bringsubviewtofront:self]; 

 
-(void) touchesmoved:(nsset *)touches withevent:(uievent *)event 

    //计算位移=当前位置-起始位置 
    cgpoint point = [[touches anyobject] locationinview:self]; 
    float dx = point.x - startpoint.x; 
    float dy = point.y - startpoint.y; 
     
    //计算移动后的view中心点 
    cgpoint newcenter = cgpointmake(self.center.x + dx, self.center.y + dy); 
     
     
    /* 限制用户不可将视图托出屏幕 */ 
    float halfx = cgrectgetmidx(self.bounds); 
    //x坐标左边界 
    newcenter.x = max(halfx, newcenter.x); 
    //x坐标右边界 
    newcenter.x = min(self.superview.bounds.size.width - halfx, newcenter.x); 
     
    //y坐标同理 
    float halfy = cgrectgetmidy(self.bounds); 
    newcenter.y = max(halfy, newcenter.y); 
    newcenter.y = min(self.superview.bounds.size.height - halfy, newcenter.y); 
     
    //移动view 
    self.center = newcenter; 

 
/* 
// only override drawrect: if you perform custom drawing. 
// an empty implementation adversely affects performance during animation. 
- (void)drawrect:(cgrect)rect 

    // drawing code 

*/ 
 
@end 


20151023105257759.gif (698×354)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网