문제

While working with the new physics engine in iOS7 using Xcode 5.1.1 I wrote the following code:

#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

As I was testing this code I discovered a very odd wiggle/jiggle. Dragging the cardView up and down, for the most part, does what is expected, i.e. the cardView glides up and down x-axis centered, but every so often the cardView moves diagonally to the left and diagonally back to the x-axis center. This wiggle doesn't seem to happen consistently but it does appear to always wiggle to the left.

Has anyone seen this before? Any ideas what cause this? How to stop it?

Thanks in advance - AYAL

도움이 되었습니까?

해결책

I tried your code and could replicate the issue. After I set the length it stopped the issue.

Try setting the length of the attachment to 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top