【问题标题】:Rotating a UIView around a center point without rotating the view itself围绕中心点旋转 UIView 而不旋转视图本身
【发布时间】:2013-08-07 19:21:43
【问题描述】:

我想在不旋转 UIView 本身的情况下围绕中心点旋转 UIView。所以更像是摩天轮的汽车(始终保持直立),而不是时钟的指针。

我围绕中心点旋转 UIViews 的时间,我使用图层位置/锚点/变换来完成效果。但这显然会改变视图本身的方向。

有什么帮助吗?

凯勒

【问题讨论】:

    标签: ios objective-c uiview quartz-core


    【解决方案1】:

    这在 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;
    }
    

    【讨论】:

      【解决方案2】:

      您应该一遍又一遍地围绕一个圆圈重新定位视图的中心。实现一个计算当前中心点的方法,并继续从 UIView animateWithDuration... 方法调用该方法。

      【讨论】:

        猜你喜欢
        • 2013-09-17
        • 1970-01-01
        • 2013-09-22
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-05
        • 2014-09-30
        相关资源
        最近更新 更多