Autolayout, UIDynamics and animations

大憨熊 提交于 2019-12-02 16:00:53

Well, similar behavior could be achieved with UIPanGestureRecognizer + [UIView animateWithDuration:animations:]. Yes, you set leading space constraint and change it according to UIPanGestureRecognizer state. Remember that you need to set final constraints only (define final position of a slider). Intermediate animation positions are calculated for you. For the slider we have default left position and activated middle position.

For view rotation we can use transform property of UIView.

Autolayout constraints in IB:

Setting animation options (UIViewAnimationOptionCurveEaseOut animation curve) could give a feel of bounce effect. UIPanGestureRecognizer code (omit instance variables declaration, because their names are self-explanatory):

- (IBAction)onPan:(UIPanGestureRecognizer*)sender
{
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            _startOffset = self.leadingSpace.constant;
            _maxOffset = self.slider.superview.frame.size.width
                - kHorizontalPadding
                - self.slider.frame.size.width;
            break;

        case UIGestureRecognizerStateChanged: {
            CGFloat offset = _startOffset + [sender translationInView:self.slider.superview].x;
            offset = MIN(offset, _maxOffset);

            self.leadingSpace.constant = offset;
            break;
        }

        case UIGestureRecognizerStateEnded: {
            CGFloat offset = _startOffset + [sender translationInView:sender.view.superview].x;
            UIColor *bgColor = [UIColor lightGrayColor];
            CGFloat rotation = 0;

            if (offset < _maxOffset) {
                offset = kHorizontalPadding;
            }
            else {
                offset = (_maxOffset + kHorizontalPadding)/2;
                bgColor = [UIColor redColor];
                rotation = M_PI_2;
            }

            self.leadingSpace.constant = offset;

            [UIView
             animateWithDuration:.5
             delay:0
             options:UIViewAnimationOptionCurveEaseOut
             animations:^{
                 [self.slider layoutIfNeeded];
                 self.slider.backgroundColor = bgColor;
                 self.slider.transform = CGAffineTransformMakeRotation(rotation);
             } completion:nil];

            break;
        }

        default:
            break;
    }
}

Animation result with UIViewAnimationOptionCurveLinear (capture simulator):

Animation result with UIViewAnimationOptionCurveEaseOut (capture simulator):

UIDynamics

With UIDynamics things become more complicated. Good starting point is Ray Wenderlich UIKit Dynamics Tutorial.

For bouncing slider we could add following behaviors:

  • UIGravityBehavior which pulls a slider to start position. We need to change angle property to direct gravity force to the left.
  • UICollisionBehavior which defines left and right edges of allowed movements. translatesReferenceBoundsIntoBoundary property will be useful if we treat parent view as boundary. Also we need to add extra boundary to stop slider in the middle using addBoundaryWithIdentifier:fromPoint:toPoint (or bezier path).
  • UIDynamicItemBehavior to change elasticy and possibly resistance properties to configure bounce and acceleration respectively.
  • Possibly UIPushBehavior in conjunction with recognizer's velocityInView: to specify slider velocity when a user releases a slider
  • Possibly UISnapBehavior as an alternative to UIGravityBehavior
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!