iPhone-如何从滚动视图中的uibutton触摸的事件滚动uiscrollview [英] iphone - how to scroll uiscrollview from event touched by uibutton in scrollview

查看:45
本文介绍了iPhone-如何从滚动视图中的uibutton触摸的事件滚动uiscrollview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构UIViewController-UIScrollview-UIButton

structure UIViewController - UIScrollview - UIButton

我要使scrollview可以接收按钮事件.因此,每当用户在按钮上进行滚动(拖动)时,scrollview都会自动滚动.

I'm going to make scrollview can receive button event. So whenever user do scrolling(dragging) on the button, the scrollview react as scrolling itself.

我按下按钮并移动了具有以下事件转发功能的处理程序

I made button down and moved handler with event forwarding like below

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

并随着触摸变化来移动滚动视图,我做了以下代码

and to move scrollview as touches change, I made below codes

- (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的反应很奇怪.当我触摸移动时,移动得还不够.

but, scrollview react strange.. not move enough as I touch moved.

推荐答案

最终答案,使用UIView动画,我得到了想要的东西.

Final answer, Using UIView animation, I got what i want.

- (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来滚动更多内容.通过提供非常短的动画持续时间,如果没有事件(按下按钮),则重复动画.它与scrollview的动画非常相似.

There is a trick..I calculated velocity by differentiating moved distance in TouchedMove function. In Button TouchUp event handler, I controlled Scrollview with velocity value and CurveEaseout animation to scroll more. By offering very short duration of animation and repeating it if there is no event(button touched down). It becomes very similar with scrollview's animation.

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

这篇关于iPhone-如何从滚动视图中的uibutton触摸的事件滚动uiscrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆