如何在scrollview中从uibutton触发的事件中滚动uiscrollview

结构 UIViewController - UIScrollview - UIButton

我要让scrollview可以接收按钮事件。 所以每当用户在按钮上滚动(拖动),滚动视图就会自动滚动。

我按下了按钮,并像下面一样移动事件转发处理程序

- (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];
}

并将scrollview作为触摸变化来移动,我在代码中做了下面的代码

- (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;
}

但是,scrollview反应奇怪..移动不够我触摸移动。


// 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);
.
.
.

最终答案,使用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功能中的移动距离来计算速度。 在Button 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];
    }
}
链接地址: http://www.djcxy.com/p/40529.html

上一篇: how to scroll uiscrollview from event touched by uibutton in scrollview

下一篇: UIButton behind UIScrollView