【发布时间】:2014-09-03 11:52:03
【问题描述】:
我正在尝试手动更改 UIDynamics 中视图的旋转,但视图的旋转总是在更新后重置。 documentation 表示,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
(我将所有代码都保留在这里,以便您可以将其复制粘贴到新创建的项目中。希望没有太多不相关的代码造成干扰)
那么我做错了吗?还是不能显式更改视图的旋转?
【问题讨论】:
标签: ios objective-c ios7 uikit-dynamics