开发者

Main view rotates from center point to 360 degree but inner views are also being rotated

Main view rotates from center point to 360 degree but inner views are also being rotated

Main view rotates from center point to 360 degree but inner views are also being rotated

I am rotating main view with 360 degrees, and I have subviews added inside main view, everything works correctly, but with one issue.

What I want to do is when I rotate main view, inner views should not lost their frames/position. Right now, when I rotate main view with infinte repeat count and dynamically if I add subview inside main view, it goe开发者_StackOverflows into proper position, but it does not retain its frame.

For example, I am implementing orbit, and for that, I have used entire transparent view as orbit and orbit is rotated from center point to 360 degree infinite times, and User can add many planets as he wants onto orbit, so when planets added on orbit, planets do not retain its frame. Can you suggest any idea?

Thanks in advance.


Well it sounds like you need to add a rotating animation for every subview that you add in your main view. If the main view rotates clockwise your subviews will need to rotate around their center in a counter-clockwise direction.


I guess you're trying to keep the subviews' orientations while rotating. If I were you, I'd use CAAnimation instead of using a view to rotate. You may add the animation to every subview, try this:

CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animation];    
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL,imgview.layer.position.x,imgview.layer.position.y);

int p =  [self getblank:tag];
float f = 2.0*M_PI  - 2.0*M_PI *p/PHOTONUM;
float h = f + 2.0*M_PI *num/PHOTONUM;
float centery = self.view.center.y;
float centerx = self.view.center.x;
float tmpy =  centery + RADIUS*cos(h);
float tmpx =    centerx - RADIUS*sin(h);
imgview.center = CGPointMake(tmpx,tmpy);

CGPathAddArc(path,nil,self.view.center.x, self.view.center.y,RADIUS,f+ M_PI/2,f+ M_PI/2 + 2.0*M_PI *num/PHOTONUM,0);    
animation.path = path;
CGPathRelease(path);
animation.duration = TIME;
animation.repeatCount = 1;
animation.calculationMode = @"paced";   
return animation;


I assume you have a stored variable that represents the rotation of the "world", correct? If not, you should.

Then, for each image you add, also store a variable with it that represents its rotation to the world.

For example, if your world is rotated 180°, and you added a cup (which you want to appear right-side up when the world is upside-down) the cup's "offset" to the world rotation would be -180°.

Then, if the world is at 180° and you rotate your world by adding 90°, then the cup's new rotation value would be cup_rotate_offset + world_rotation, or 180°+270°, which is the same as saying 90°, and the top of the world would be facing left and the cup's top would be facing right.

You have to independently track the offset values for each added object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜