Objective c 连续旋转

Objective c 连续旋转,objective-c,rotation,uiimage,nstimer,Objective C,Rotation,Uiimage,Nstimer,我有几行代码更改按钮的图像,然后旋转它。如何使图像在计时器继续增加时继续旋转。另外,当重置NSTimer时,如何停止旋转并将图像更改回原位?下面是一些代码: -(void)tick{ timeTick++; NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick]; labelTime.text = timeString; UIImage* timebuttonImg =[U

我有几行代码更改按钮的图像,然后旋转它。如何使图像在计时器继续增加时继续旋转。另外,当重置
NSTimer
时,如何停止旋转并将图像更改回原位?下面是一些代码:

-(void)tick{
    timeTick++;
    NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
    labelTime.text = timeString;

    UIImage* timebuttonImg =[UIImage imageNamed:@"icon.png"];
    [timebutton setImage:timebuttonImg forState:UIControlStateNormal];
    timebutton.transform = CGAffineTransformMakeRotation(4);

}

timebutton.transform是正确的方向

“变换”特性相对于其原始位置(在任何变换之前)对其进行变换。要连续旋转它,您需要以下内容:

- (void)tick:(NSTimer *)timer
{
    // update label if you want

    self.angle += 0.1; // increment it by whatever you want (measured in radians)
    if (self.angle > (M_PI * 2.0))
        self.angle = fmodf(self.angle, M_PI * 2.0);

    self.button.transform = CGAffineTransformMakeRotation(self.angle);
}
timebutton.transform=CGAffineTransformMakeRotation(timeTick*2*M_PI/);//2*M_PI是一个圆

要重置它,请执行以下操作:

timebutton.transform=CGAffineTransformity//根据Rob的答案进行编辑

0的旋转表示“使用原始变换矩阵”

注:

它围绕其定位点旋转(可以设置属性)。定位点的比例为0.0-1.0,其中1.0是视图图层的高度/宽度

例如:定位点(1.0,0.0)围绕视图图层的右上角居中

编辑:

NumTotalTicks->TicksPerCircle//更清晰的命名

一些想法:

>P>如果你不需要使用计时器,考虑只使用动画。要开始旋转,请执行以下操作:

CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = @(0.0);
rotationAnimation.toValue = @(M_PI * 2.0);
rotationAnimation.duration = 1.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VALF;

[self.button.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
要停止动画,请执行以下操作:

[self.button.layer removeAnimationForKey:@"rotationAnimation"];
停止动画后,如果要将其动画化回原始位置:

CALayer *currentLayer = (CALayer *)[self.button.layer presentationLayer];
CGFloat  currentAngle = [(NSNumber *)[currentLayer valueForKeyPath:@"transform.rotation.z"] floatValue];

self.button.transform = CGAffineTransformMakeRotation(currentAngle);

[UIView animateWithDuration:0.25 animations:^{
    self.button.transform = CGAffineTransformIdentity;
}];
  • 如果确实要使用
    NSTimer
    ,可以执行以下操作来启动计时器:

    self.angle = 0.0;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    self.timer = timer;
    
    [self.timer invalidate];
    self.timer = nil;
    [UIView animateWithDuration:0.25 animations:^{
        self.button.transform = CGAffineTransformIdentity;
    }];
    
    这显然假设您具有
    角度
    计时器的属性:

    @property (weak, nonatomic) NSTimer  *timer;
    @property (nonatomic)       CGFloat   angle;
    
    勾选:
    例程可以执行如下操作:

    - (void)tick:(NSTimer *)timer
    {
        // update label if you want
    
        self.angle += 0.1; // increment it by whatever you want (measured in radians)
        if (self.angle > (M_PI * 2.0))
            self.angle = fmodf(self.angle, M_PI * 2.0);
    
        self.button.transform = CGAffineTransformMakeRotation(self.angle);
    }
    
    要停止计时器,请执行以下操作:

    self.angle = 0.0;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    self.timer = timer;
    
    [self.timer invalidate];
    self.timer = nil;
    [UIView animateWithDuration:0.25 animations:^{
        self.button.transform = CGAffineTransformIdentity;
    }];
    
    并且不要忘记在视图关闭时调用停止计时器代码(例如,在
    视图中消失
    ),因为如果在视图关闭时计时器仍在运行,它将保持对视图控制器的强引用

  • 也许更好的方法是,使用经过的时间来计算旋转,以确保恒定的旋转速度,而不管
    NSTimer
    调用的频率如何(因为您不能绝对确定这些
    NSTimer
    调用的频率)。因此,定义
    计时器
    开始时间
    的属性:

    @property (weak, nonatomic) NSTimer       *timer;
    @property (nonatomic)       CFAbsoluteTime startTime;
    
    self.startTime = CFAbsoluteTimeGetCurrent();
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    self.timer = timer;
    
    要启动
    计时器
    并设置
    启动时间

    @property (weak, nonatomic) NSTimer       *timer;
    @property (nonatomic)       CFAbsoluteTime startTime;
    
    self.startTime = CFAbsoluteTimeGetCurrent();
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    self.timer = timer;
    
    然后勾选:
  • - (void)tick:(NSTimer *)timer
    {
        CGFloat elapsed = CFAbsoluteTimeGetCurrent() - self.startTime;
        CGFloat angle = fmodf(elapsed * kRotationsPerSecond, 1.0) * M_PI * 2.0;
    
        self.button.transform = CGAffineTransformMakeRotation(angle);
    }
    
    停止计时器代码如上所示

    但是,如果旋转速度很关键(例如,你正在做一个扫秒针),请参考设备的时钟来计算旋转度


    我放进去干什么?一个圆圈里需要多少个记号。如果你有一个时钟,并且你正在旋转时针,你需要12作为你的指针。明白了。我还希望图像围绕其中心旋转。轴心点当前设置为一些奇怪的东西。如何将其更改为图像的中心?将定位点设置为(0.5,0.5)。我相信代码是这样的:
    [[timebutton layer]setancorpoint:CGPointMake(0.5,0.5)]即使使用代码,图像仍会四处移动。为了澄清,我希望图像在不改变位置的情况下旋转。思想?