簡體   English   中英

圍繞中心點旋轉UIView而不旋轉視圖本身

[英]Rotating a UIView around a center point without rotating the view itself

我想在不旋轉UIView本身的情況下圍繞中心點旋轉UIView。 所以更像是摩天輪的車(總是保持直立),而不是鍾表的手。

我圍繞中心點旋轉UIViews的時間,我使用了圖層位置/ anchorPoint /變換來完成效果。 但這顯然會改變視圖本身的方向。

有幫助嗎?

凱勒

在Core Animation中這很容易做到。 我在這個例子中使用了一些相當無聊的靜態值,所以很明顯你會想做一些修改。 但是這將向您展示如何沿着圓形UIBezierPath移動視圖。

UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[view setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
[view setCenter:[self pointAroundCircumferenceFromCenter:self.view.center withRadius:140.0f andAngle:0.0f]];
[self.view addSubview:view];

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(CGRectGetMidX(self.view.frame) - 140.0f, CGRectGetMidY(self.view.frame) - 140.0f, 280.0f, 280.0f)];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 5.0;

pathAnimation.path = [path CGPath];

[view.layer addAnimation:pathAnimation forKey:@"curveAnimation"];

並且是生成視圖初始中心的基本功能。

- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center withRadius:(CGFloat)radius andAngle:(CGFloat)theta
{
    CGPoint point = CGPointZero;
    point.x = center.x + radius * cosf(theta);
    point.y = center.y + radius * sinf(theta);

    return point;
}

您應該一遍又一遍地圍繞圓圈重新定位視圖的中心。 實現一個計算當前中心點的方法,並繼續從UIView animateWithDuration ...方法調用該方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM