自动版式,UIDynamics和动画 [英] Autolayout, UIDynamics and animations
问题描述
我是pretty新的自动布局,我感到困惑如何动画的看法。
I'm pretty new to auto layout and I'm confused about how to animate views.
我读了很多,我知道,你必须坚持的限制,编辑它,并在的UIView $包裹
layoutIfNeeded
C $ C>动画块。
I read a lot, and I know you must hold to the constraints, edit it, and wrap the layoutIfNeeded
in an UIView
animation block.
但是,当涉及到做到这一点,我有点失落。我很乐意,如果有人可以解释我如何这个动画是例如完成的。
But when it comes to do it, I'm a little lost. I'd love if someone could explain me how this animation is done for example.
我想大概使用了 UIPanGestureRecognizer
更改恒
领先的空间容器的限制,但它可能(在正确的反弹效果?)使用UIDynamics。
I think it probably uses a UIPanGestureRecognizer
to change the constant
of the leading space to container constraint, but it probably uses UIDynamics (for the bounce effect at the right ?).
推荐答案
那么,类似的行为可能与 UIPanGestureRecognizer
+ 实现[UIView的animateWithDuration:动画:]
。是的,您可以设置按 UIPanGestureRecognizer
状态导致空间限制和改变它。请记住,你只需要设置最后的约束(定义滑块的最终位置)。中间的动画位置为您计算。滑块我们的默认的左侧位置和激活的中间位置。
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.
有关视图旋转,我们可以使用变换
的属性的UIView
。
For view rotation we can use transform
property of UIView
.
在IB自动布局约束:
设置动画选项( UIViewAnimationOptionCurveEaseOut
动画曲线)可以给的反弹效应的感觉。 UIPanGestureRecognizer
code(省略实例变量声明,因为它们的名称是不言自明):
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;
}
}
使用动画效果 UIViewAnimationOptionCurveLinear
(捕获模拟器):
使用动画效果 UIViewAnimationOptionCurveEaseOut
(捕获模拟器):
Animation result with UIViewAnimationOptionCurveEaseOut
(capture simulator):
UIDynamics
使用UIDynamics事情变得更加复杂。良好的出发点是雷Wenderlich UIKit的动态教程。
With UIDynamics things become more complicated. Good starting point is Ray Wenderlich UIKit Dynamics Tutorial.
有关反弹滑块我们可以添加以下行为:
For bouncing slider we could add following behaviors:
-
UIGravityBehavior
这拉滑盖启动位置。我们需要改变角度
属性指示重力左侧。 -
UICollisionBehavior
定义允许的动作左右两边。如果我们把父视图为界translatesReferenceBoundsIntoBoundary
属性将是有益的。此外,我们需要添加额外的边界用停在中间的滑块addBoundaryWithIdentifier:fromPoint:尖山
(或贝塞尔路径) -
UIDynamicItemBehavior
修改elasticy
和可能电阻
属性分别配置弹跳和加速度。 - 可能
UIPushBehavior
与识别的velocityInView结合:
当用户释放滑块指定滑块速度 - 可能
UISnapBehavior
来替代UIGravityBehavior
UIGravityBehavior
which pulls a slider to start position. We need to changeangle
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 usingaddBoundaryWithIdentifier:fromPoint:toPoint
(or bezier path).UIDynamicItemBehavior
to changeelasticy
and possiblyresistance
properties to configure bounce and acceleration respectively.- Possibly
UIPushBehavior
in conjunction with recognizer'svelocityInView:
to specify slider velocity when a user releases a slider - Possibly
UISnapBehavior
as an alternative toUIGravityBehavior
这篇关于自动版式,UIDynamics和动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!