gpt4 book ai didi

ios - UIDynamicAnimator/UIAttachmentBehavior 抖动?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:17 24 4
gpt4 key购买 nike

在使用 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;

关于ios - UIDynamicAnimator/UIAttachmentBehavior 抖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com