3

just like the path in the picture

(new users aren't allowed to post images, sorry i can only give the link)

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

Thanks a lot for any help

icefox
  • 149
  • 1
  • 14

2 Answers2

3

Try this on touches event delegate (if you want to rotate the view with touch event)-

- (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;
  } 

use transform rotation , and tell me if it works..

Naina Soni
  • 720
  • 5
  • 21
2

Please look at my answer for this question about "rotating a label around an arbitrary point" for a more detailed explanation about using the an anchor point when rotating (ignore the keyframe animation answers since they are more complicated and not the "right tool for this job" even though the answers says so).

In short: set the anchor point to the point (in the unit-coordinate system of your views layer) to the point outside of your view and just apply a normal rotation animation.


EDIT: One thing to remember

The frame of your layer (which is also the frame of your view) is calculated using the position, bounds and anchor point. Changing the anchorPoint will change where your view appears on screen. You can counter this by re-setting the frame after changing the anchor point (this will set the position for you). Otherwise you can set the position to the point you are rotating to yourself. The Layer Geometry and Transforms (linked to in the other question) also mentions this.


The code that I provided in that answer (generalized to a UIView and Core Animation instead of UIView-animations):

#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"]  
}
Community
  • 1
  • 1
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • Actually, I don't think that will work for the OP's specific goal. The anchorPoint property is in the range (0,0) to (1,1) and must be inside the image/layer you are rotating. Unless you have an image/layer that is bigger than the visible contents that you are rotating, with the rest of the view/layer being transparent, you can't make the point of rotation be outside of the image. – Duncan C May 25 '12 at 01:36
  • I think the OP will have to create a transformation matrix that shifts, then rotates in order to get a rotation around a point outside the image (or is that rotate, then shift? I always get the order wrong when concatenating shifts with rotates.) – Duncan C May 25 '12 at 01:37
  • @DuncanC The docs only mention that the anchor point is "Described in the unit coordinate space" but that shouldn't mean that you can't set it outside the range (0,0) and (1,1). In fact it seems to work fine with an anchor point outside that range exempt that it changes the position of the view. You can counteract that by either setting the position to the point you are rotating about or making a translate-rotate-translate transform (though then the animation will move in a straight line and not keep the distance from the rotation point). I will update my answer with this information. – David Rönnqvist May 25 '12 at 06:18
  • @DavidRönnqvist, how can I animately reverse this to get back to the initial state? – Rizon Aug 10 '13 at 14:46
  • @Rizon Use the negative angle for the rotation. That will cause a rotation in the other direction. – David Rönnqvist Aug 11 '13 at 16:07
  • How do I prevent it from returning back to the original point? I want it to stay at the new point after the animation has ended. – Aviram Jan 13 '15 at 13:35
  • @Aviram Set the final value on the layer before adding the animation. Depending on the animation you may have to specify a combination of to/from to make it look the same. This is a different question (that has been asked before) so I. won't elaborate further in this comment – David Rönnqvist Jan 13 '15 at 13:37