簡體   English   中英

UIDynamicAnimator/UIAttachmentBehavior 抖動?

[英]UIDynamicAnimator/UIAttachmentBehavior jiggle?

在使用 Xcode 5.1.1 在 iOS7 中使用新的物理引擎時,我編寫了以下代碼:

#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

我試過你的代碼,可以復制這個問題。 在我設置長度后,它停止了問題。

嘗試將附件的長度設置為 1

case UIGestureRecognizerStateBegan:
     self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.cardView attachedToAnchor:touchPoint];
     self.attachment.length = 1;
     [self.animator addBehavior:self.attachment];
     break;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM