舉例講解iOS開發中拖動視圖的實現_IOS

來源:互聯網
上載者:User

預備知識
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 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.