我正在尝试手动更改UIDynamics
视图的旋转,但视图的旋转总是在更新后重置。文档说, that的方法应该更新项目UIDynamicAnimator
的updateItemUsingCurrentState:
位置和旋转,但只更新位置。
我创建了一个简短的示例,其中一个方形视图从屏幕上落下,触摸后它应该定位到触摸的位置并旋转 45 度。但是,不会发生旋转(有时旋转可以在几分之一秒内看到,但之后会再次重置):
#import "ViewController.h"
@interface ViewController ()
{
UIDynamicAnimator* _animator;
UIView* _square;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_square = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
_square.backgroundColor = [UIColor grayColor];
[self.view addSubview:_square];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[_square]];
gravityBehavior.magnitude = 0.1;
[_animator addBehavior:gravityBehavior];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
_square.center = [touch locationInView:self.view];
NSLog(@"transform before rotation: %@", NSStringFromCGAffineTransform(_square.transform));
_square.transform = CGAffineTransformMakeRotation(M_PI/4);
[_animator updateItemUsingCurrentState:_square];
NSLog(@"transform after rotation: %@", NSStringFromCGAffineTransform(_square.transform));
}
@end
(我把所有的代码都留在这里了,这样你就可以把它复制粘贴到一个新创建的项目中。希望没有太多不相关的代码让人不安)
那我做错了吗?或者是否可以显式更改视图的旋转?