Question

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

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top