Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How does <code>UIAttachmentBehavior</code> affect a view?</p> <p>It does it by modifying the view's transform.</p> <p>In this <a href="http://www.raywenderlich.com/50197/uikit-dynamics-tutorial" rel="nofollow">Ray Wenderlich tutorial</a> Colin Eberhardt logs the transform as the view is affected by the behavior. </p> <p>Even though the transform is never set, or modified directly, it changes as the view is moved by the behavior.</p> <p>So you can't set your transform, and hope for it to stay set, because it is being set by the behavior.</p> <p>In a side note, if you are trying to set the transform to scale by 1.5, you should use this:</p> <pre><code>self.transform = CGAffineTransformMakeScale(1.5, 1.5); </code></pre> <p>otherwise every time your <code>touchesBegan</code> is called it will grow by another 50%.</p> <p>In the documentation for UIDynamicAnimator, its says:</p> <blockquote> <p>"A dynamic animator automatically reads the initial state (position and rotation) of each dynamic item you add to it, and then takes responsibility for updating the item’s state. If you actively change the state of a dynamic item after you’ve added it to a dynamic animator, call this method to ask the animator to read and incorporate the new state."</p> </blockquote> <p>So just call your transform after adding the behavior, then call updateItemUsingCurrentState:</p> <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.referenceView]; _touchAttachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self attachedToAnchor:touchLocation]; [_animator addBehavior:_touchAttachmentBehavior]; self.transform = CGAffineTransformMakeScale(1.5, 1.5); [_animator updateItemUsingCurrentState:self]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload