2012-05-21 56 views
2

結構 的UIViewController - UIScrollView中 - 的UIButtoniphone - 如何滾動由UIButton的感動事件UIScrollView的滾動型

我要做出滾動視圖可以接收按鈕事件。所以每當用戶在按鈕上滾動(拖動),滾動視圖就會自動滾動。

我做了按鈕來與事件轉發像下面

- (IBAction)buttonTouchedMove:(id)sender withEvent:(UIEvent *)event { 
    [[sender nextResponder] touchesMoved:[event allTouches] withEvent:event]; 
} 
- (IBAction)buttonTouchedDown:(id)sender withEvent:(UIEvent *)event { 
    [[sender nextResponder] touchesBegan:[event allTouches] withEvent:event]; 
} 

和觸摸改變移動滾動型移動處理,我做了下面的代碼

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint offset = self.scrollView1.contentOffset; 
    UITouch *touch = [touches anyObject]; 
    self.newPoint = [touch locationInView:self.view]; 
    int diffX = newPoint.x - oldPoint.x; 
    offset.x = offset.x - diffX; 
    [scrollView1 setContentOffset:offset animated:YES]; 
    self.oldPoint = self.newPoint; 
} 

但是,滾動型反應奇怪..不移動,因爲我觸摸移動。

+0

嘗試類似這樣的事情int diffX = 10 *(newPoint.x - oldPoint.x);這是因爲我thnk你的按鈕框架小於視圖的 – Saad

+0

謝謝,它的工作原理。但是,當我拖動它時,scrollview會發抖.. – ytkang

+0

檢查您是否在其他地方設置了內容偏移量 – Saad

回答

0

最終答案,使用UIView動畫,我得到了我想要的。

- (IBAction)buttonTouchedDown:(id)sender withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    self.oldPoint = [touch locationInView:self.view]; 
    self.velocity = 0; 
    isButtonTouchedDown = YES; 
} 

- (IBAction)buttonTouchedMove:(id)sender withEvent:(UIEvent *)event { 
    CGPoint offset = self.scrollView1.contentOffset; 
    UITouch *touch = [[event allTouches] anyObject]; 
    self.newPoint = [touch locationInView:self.view]; 
    int diffX = self.newPoint.x - self.oldPoint.x; 
    velocity = diffX; 

    offset.x = offset.x - diffX; 
    [self.scrollView1 setContentOffset:offset animated:NO]; 
    self.oldPoint = self.newPoint; 
} 

有一個技巧..我通過區分TouchedMove函數中的移動距離來計算速度。 在按鈕TouchUp事件處理程序中,我使用速度值和CurveEaseout動畫控制滾動查看滾動更多。通過提供非常短的動畫持續時間,並在沒有事件(按下按鈕)時重複播放。它與scrollview的動畫非常相似。

- (IBAction)buttonTouchedUp:(id)sender withEvent:(UIEvent *)event { 
    CGPoint offset = self.scrollView1.contentOffset; 
    amountX = (self.velocity)*(abs(self.velocity)); 
    dX = amountX; 
    isButtonTouchedDown = NO; 
    if (offset.x - amountX < 0) { 
     offset.x = 0; 
     [scrollView1 setContentOffset:offset animated:YES]; 
    } 
    else if (abs(dX) < 70) { // 70: content item size 
     offset.x = roundf(offset.x/70)*70; 
     [scrollView1 setContentOffset:offset animated:YES]; 
    } 
    else { 
     [self endScrollAnimation]; 
    } 

- (void)startAnimation:(float)x moveAmount:(float)moveAmount 
{ 
    CGPoint offset = self.scrollView1.contentOffset; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:duration]; 

    offset.x = x < 0 ? offset.x + moveAmount : offset.x -moveAmount; 

    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(endScrollAnimation)]; 
    [scrollView1 setContentOffset:offset animated:NO]; 
    [UIView commitAnimations]; 

} 

- (void)endScrollAnimation 
{ 
    CGPoint offset = self.scrollView1.contentOffset; 
    float slowMoveAmount = abs(amountX) * 0.05; 
    float fastMoveAmount = abs(amountX) * 0.1; 
    if (isButtonTouchedDown) { 
     return; 
    } 
    else if (abs(dX) > abs(amountX)*0.35 && offset.x > 0) { 
     [self startAnimation:dX moveAmount:fastMoveAmount]; 
     dX = dX < 0 ? dX + fastMoveAmount : dX -fastMoveAmount; 
    } 
    else if (abs(dX) > slowMoveAmount && offset.x > 0) { 
     [self startAnimation:dX moveAmount:slowMoveAmount]; 
     dX = dX < 0 ? dX + slowMoveAmount : dX - slowMoveAmount; 
    } 
    else { 
     offset.x = roundf(offset.x/70)*70; 
     [scrollView1 setContentOffset:offset animated:YES]; 
    } 
} 
+0

如果有人對此有疑問。不要給我發電子郵件。 :) *我可以閱讀英文和韓文兩種 – ytkang

0
// get the button width 

CGFloat buttonWidth = self.theButton.frame.size.width; 
// replace your button name here 


// now get the view width 

CGFloat viewWidth = self.view.frame.size.width; 
// replace your view name here 

// now get the multiplier which i said u as 10 

CGFloat mult = viewWidth/buttonWidth; 

// and now use it in your code 

. 
. 
. 
diffX = mult*(newPoint.x - oldPoint.x); 
. 
. 
. 
+0

感謝您的回答。但我的問題不是來自diffX。 我得到了結果,我想通過刪除setContentOffset動畫:否不是。 但..沒有按鈕滾動沒有慣性效果.. – ytkang