使用CGPathAddArc使UIView的Layer绕中心点旋转

        float angle = 360 / 6;
        for (int i = 0; i < 6; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            float offsetAngle = angle * i;
            button.bounds = CGRectMake(0, 0, 32, 32);
            button.center = CGPointMake(cos(offsetAngle * RADIANS) * 160, sin(offsetAngle * RADIANS) * 160);
            button.tag = i;
            CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            CGMutablePathRef path = CGPathCreateMutable();
            pathAnimation.calculationMode = kCAAnimationPaced;
            pathAnimation.fillMode = kCAFillModeForwards;
            pathAnimation.removedOnCompletion = NO;
            pathAnimation.duration = 2;
            CGPathMoveToPoint(path, NULL, button.center.x, button.center.y);
            CGPathAddArc(path, NULL, 0, 0, 160, offsetAngle * M_PI/180, offsetAngle * M_PI/180 + M_PI, YES);
            pathAnimation.path = path;
            CGPathRelease(path);
            [button.layer addAnimation:pathAnimation forKey:@"curve"];
            [tabBarView addSubview:button];
        }

因为只是Layer的内容发生的位置变化,其实UIView还是在原来的位置上,如果需要使用应该在Animation结束后重新设定UIView的位置.


顺便翻译一下原文:

CGPathAddArc (

   CGMutablePathRef path,

   const CGAffineTransform *m,

   CGFloat x,

   CGFloat y,

   CGFloat radius,

   CGFloat startAngle,

   CGFloat endAngle,

   bool clockwise

);

path:动画的路径;

m:layer的transform;

x:其实x和y是什么我还没弄明白,根据原文应该是弧的圆心点,但我设置过,这更像是偏移量;

y:同上;

startAngle:起始的角度点,零度是与x轴相交点,度数为顺时针;

endAngle:结束的角度点;

clockwis:是否顺时针.

好的,我理解了你的要求。你可以通过以下Objective-C代码实现该功能: 首先,在你的UIViewController中添加以下属性: ``` @property (nonatomic, assign) BOOL isFullScreen; @property (nonatomic, strong) UITapGestureRecognizer *tapGesture; ``` 然后,在你的UIViewController的viewDidLoad方法中添加如下代码: ``` self.isFullScreen = NO; self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [self.view addGestureRecognizer:self.tapGesture]; ``` 接下来,实现handleTapGesture方法: ``` - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { if (self.isFullScreen) { [UIView animateWithDuration:0.3 animations:^{ self.view.transform = CGAffineTransformIdentity; self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); }]; self.isFullScreen = NO; } else { [UIView animateWithDuration:0.3 animations:^{ self.view.transform = CGAffineTransformMakeRotation(M_PI_2); self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); }]; self.isFullScreen = YES; } } ``` 这里我们使用UIView的transform属性来实现旋转使用CGAffineTransformMakeRotation方法来创建旋转变换矩阵。同时,我们还改变了UIView的frame属性来使其全屏显示。 当用户点击UIView时,我们会先判断当前UIView是否已经全屏显示,如果已经全屏显示,则执行恢复原样的动画;否则,执行全屏显示的动画。 希望这个代码能够对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值