ios中建立可以拖動的view原理和實現詳解(含代碼)

來源:互聯網
上載者:User

標籤:使用   os   width   io   問題   re   

有時候我們會需要在介面上拖動view;uiview是繼承於uiresponder的,所以可以響應觸摸相關的事件。

重點是以下一組方法:

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


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


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event     // 觸摸事件結束,如果你需要自動把view停靠到一個位置,實現這個方法


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  //外界因素取消touch事件等,如進入電話,進行特別處理


對於最上面兩個方法是必須實現的,後面兩個方法是用來做一些額外的需求或者處理使用,如果只是要實現拖動view可以不實現。


思路1: 建立一個uiview(或者你需要的控制項)的子類,在類中實現上述的方法。

思路2:在你相應的viewcontroller中實現上述方法(在viewcontroller中持有你要拖動的view,這樣才能控制它),也能實作類別似的目的,但這樣觸摸的範圍就會是整個viewcontroller的view,你需要在touchesBegan進行相應的判斷(從UITouch中可以得到view的相關資訊),才能實現固定在小視窗內部的觸摸。

兩種思路都是可行的,根據你實際情況去做選擇,都沒有問題。

以下是代碼(子類方式的簡單實現,你也可以進行相應修改放到viewcontroller中):

@interface TouchEaglView()
@property (assign, nonatomic) CGPoint beginpoint;
@end
@implementation TouchEaglView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    self.beginpoint = [touch locationInView:self];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];
    CGRect frame = self.frame;
    frame.origin.x += currentLocation.x - self.beginpoint.x;
    frame.origin.y += currentLocation.y - self.beginpoint.y;
    self.frame = frame;
}


上面的代碼存在一個問題,那就是他的觸摸移動範圍包括了螢幕之外,你會發現你可以把view部分拖動到螢幕外部。那麼我們需要一個進階一些的實現:

註:這個版本是基於viewcontroller的實現,並未子類化view;self.localview是你持有的小視窗,beginpoint需要你在viewcontroller中自己定義

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.isInView)    // 僅當取到touch的view是小視窗時,我們才響應觸控,否則直接return
    {
        return;
    }
    
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.localView];
    //位移量
    float offsetX = currentPosition.x - beginpoint.x;
    float offsetY = currentPosition.y - beginpoint.y;
    //移動後的中心座標
    self.localView.center = CGPointMake(self.localView.center.x + offsetX, self.localView.center.y + offsetY);
    
    //x軸左右極限座標
    if (self.localView.center.x > (self.localView.superview.frame.size.width-self.localView.frame.size.width/2))
    {
        CGFloat x = self.localView.superview.frame.size.width-self.localView.frame.size.width/2;
        self.localView.center = CGPointMake(x, self.localView.center.y + offsetY);
    }
    else if (self.localView.center.x < self.localView.frame.size.width/2)
    {
        CGFloat x = self.localView.frame.size.width/2;
        self.localView.center = CGPointMake(x, self.localView.center.y + offsetY);
    }
    
    //y軸上下極限座標
    if (self.localView.center.y > (self.localView.superview.frame.size.height-self.localView.frame.size.height/2))
    {
        CGFloat x = self.localView.center.x;
        CGFloat y = self.localView.superview.frame.size.height-self.localView.frame.size.height/2;
        self.localView.center = CGPointMake(x, y);
    }
    else if (self.localView.center.y <= self.localView.frame.size.height/2)
    {
        CGFloat x = self.localView.center.x;
        CGFloat y = self.localView.frame.size.height/2;
        self.localView.center = CGPointMake(x, y);
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view.frame.size.width == 120)   // 120為小視窗的寬度(簡單起見這裡使用寫入程式碼樣本),用來判斷觸控範圍;僅當取到touch的view是小視窗時,我們才響應觸控
    {
        self.isInView = YES;
    }
    else
    {
        self.isInView = NO;
    }
    beginpoint = [touch locationInView:self.localView];
    
    [super touchesBegan:touches withEvent:event];
}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.