2
使用的Xcode 5.1.1 iOS7新的物理引擎工作时我写了下面的代码:UIDynamicAnimator/UIAttachmentBehavior微动?
#import "ZViewController.h"
@interface ZViewController()
@property (nonatomic, strong) UIView *cardView;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIAttachmentBehavior *attachment;
@end
@implementation ZViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.cardView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
self.cardView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.cardView];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureHandler:)];
[self.cardView addGestureRecognizer:panGestureRecognizer];
self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.cardView.center = self.view.center;
}
- (void)panGestureHandler:(UIPanGestureRecognizer *)panGesture{
CGPoint touchPoint = [panGesture locationInView:self.view];
switch(panGesture.state){
case UIGestureRecognizerStateBegan:
self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.cardView attachedToAnchor:touchPoint];
[self.animator addBehavior:self.attachment];
break;
case UIGestureRecognizerStateChanged:
touchPoint.x = 160.0;
self.attachment.anchorPoint = touchPoint;
break;
case UIGestureRecognizerStateEnded:
[self.animator removeBehavior:self.attachment];
self.attachment = nil;
break;
default:
break;
}
}
@end
正如我在测试这个代码,我发现了一个非常奇怪的扭动/轻摇。向上和向下拖动cardView,大部分都是做预期的事情,即cardView上下滑动x轴居中,但cardView往往对角地向左移动并斜向回到x轴中心。这种摆动似乎并不一致,但似乎总是摆动到左侧。
有没有人见过这个?任何想法是什么原因造成的如何阻止它?
在此先感谢 - AYAL
这做到了!多么奇怪。未定义的附件长度必须对物理引擎造成奇怪的影响。谢谢 – Ayal