我在家庭视图控制器中有三个按钮。当我点击but1时,它执行动画并向右移动,持续时间为特定宽度。当but1达到它给定的宽度时,它隐藏并显示but2那里。当我点击but2它执行动画并移回到but1的相同位置。它就像打开和关闭按钮的场景。我现在面临的问题是,当我打开但是它会移动它给定的宽度,当我关闭它时会移回原来的but1位置,现在按钮打开和关闭但是当我们再次尝试打开时它不会打开但是。我希望每当用户点击but1时它应该向右移动动画,每当用户想要关闭按钮时它应该关闭它而不是只打开或关闭一次。我打开按钮的代码就是这个,
- (IBAction)openHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
CGAffineTransform transform = CGAffineTransformMakeTranslation(108, 0);
[_openHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:NO];
}
-(void)targetMethod:(NSTimer *)myTimer{
_home.hidden=NO;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
关闭按钮,
- (IBAction)closeHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-110, 0);
[_closeHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:@selector(targetMethoded:)
userInfo:nil
repeats:NO];
}
-(void)targetMethoded:(NSTimer *)myTimer{
_home.hidden=YES;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
答案 0 :(得分:0)
不使用CGAffineTransform更改位置,只需直接更改按钮框即可。还可以使用以下代码,该代码使用完成处理程序来调用targetMethod而不是使用NSTimer:
[UIView animateWithDuration:0.75 animations:^{
_openHome.frame = CGRectMake(_openHome.frame.origin.x+110, _openHome.frame.origin.y, _openHome.frame.size.width, _openHome.frame.size.height);
} completion:^(BOOL finished) {
[self targetMethod];
}];