How to move uiview along the trajectory of a circle with rotation?

same as the path in the picture

(new users are not allowed to send images, sorry, I can give a link)

http://cdn.dropmark.com/30180/3bd0f0fc6ee77c1b6f0e05e3ea14c821f8b48a82/questiong1-1.jpg

Thanks so much for any help

+3
source share
2 answers

Please see my answer to this question about “label rotation around an arbitrary point” for a more detailed explanation of the use of an anchor point during rotation (ignoring keyframe animation answers because they are more complicated and not “the right tool for this job”, even if the answers are they say).

: ( ) .


: ,

( ) , . , . , ( ). , . Layer Geometry and Transforms ( ) .


, ( UIView Core Animation UIView-):

#define DEGREES_TO_RADIANS(angle) (angle/180.0*M_PI)

- (void)rotateView:(UIView *)view 
       aroundPoint:(CGPoint)rotationPoint 
          duration:(NSTimeInterval)duration 
           degrees:(CGFloat)degrees {

    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    [rotationAnimation setDuration:duration];
    // Additional animation configurations here...

    // The anchor point is expressed in the unit coordinate
    // system ((0,0) to (1,1)) of the label. Therefore the 
    // x and y difference must be divided by the width and 
    // height of the view (divide x difference by width and 
    // y difference by height).
    CGPoint anchorPoint = CGPointMake((rotationPoint.x - CGRectGetMinX(view.frame))/CGRectGetWidth(view.bounds),
                                      (rotationPoint.y - CGRectGetMinY(view.frame))/CGRectGetHeight(view.bounds));

    [[view layer] setAnchorPoint:anchorPoint];
    [[view layer] setPosition:rotationPoint]; // change the position here to keep the frame
    CATransform3D rotationTransform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(degrees), 0, 0, 1);
    [rotationAnimation setToValue:[NSValue valueWithCATransform3D:rotationTransform]];

    // Add the animation to the views layer
    [[view layer] addAnimation:rotationAnimation
                        forKey:@"rotateAroundAnchorPoint"]  
}
+2

( ) -

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
    touches = [event allTouches];
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint Pageposition = [touch locationInView:self];

    CGPoint prevPoint = [[[touches allObjects] objectAtIndex:0] previousLocationInView:self];
    CGPoint curPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self];

    float prevAngle = atan2(prevPoint.x, prevPoint.y);
    float curAngle= atan2(curPoint.x, curPoint.y);
    float angleDifference = curAngle - prevAngle;

    CGAffineTransform newTransform3 = CGAffineTransformRotate(self.transform, angleDifference);
    self.transform = newTransform3;
  } 

, .

+3

All Articles