• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIDynamicAnimator/UIAttachmentBehavior 抖动?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:28:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在使用 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 actionselector(panGestureHandler];
    [self.cardView addGestureRecognizer:panGestureRecognizer];

    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
}

- (void)viewWillAppearBOOL)animated{
    [super viewWillAppear:animated];

    self.cardView.center = self.view.center;
}

- (void)panGestureHandlerUIPanGestureRecognizer *)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



Best Answer-推荐答案


我尝试了您的代码并且可以复制该问题。在我设置长度后,它停止了问题。

尝试将附件的长度设置为 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap