我有一个NSTimer,可以持续触发advavanceTimer:
方法,在剩下的秒数内更新倒计时UILabel
。
同时CMMotionManager
检测到摇动手势并将UIView
对象50px移动到右侧,或者说动画不会持续存在。
问题:已解雇的advavanceTimer:
方法将UIView
移回原点位置
-(void)startCountdownWithTimeLimit:(double)timeLimit {
self.counter = timeLimit;
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(advanceTimer:)
userInfo:nil
repeats:YES];
}
-(void)advanceTimer:(NSTimer*)timer {
[self setCounter:(self.counter-1)];
self.countdownLabel.text = [NSString stringWithFormat:@"Countdown: %@", [NSNumber numberWithInt:self.counter]]; //if removed, everything works fine
//...
}
-(void)setupMotionManager{
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 0.1;
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error)
{
[self motionMethod:motion];
}];
}
-(void)motionMethod:(CMDeviceMotion *)deviceMotion
{
CMAcceleration userAcceleration = deviceMotion.userAcceleration;
if (fabs(userAcceleration.x) > accelerationThreshold
|| fabs(userAcceleration.y) > accelerationThreshold
|| fabs(userAcceleration.z) > accelerationThreshold)
{
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{
CGRect oldFrame = self.movingView.frame;
CGRect newFrame = CGRectMake(oldFrame.origin.x+50, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);
self.movingView.frame = newFrame;
} completion:^(BOOL finished) {
}];
}
}
答案 0 :(得分:1)
问题是您正在使用自动布局。每次设置标签文本时,自动布局都会执行布局。因此,在那一刻,约束出现并将一切都放回到由这些约束所决定的位置。
解决方案:不要更改(动画)任何内容的框架;更改(动画)其约束。