我有 2 个按钮,我按住它们,它们在屏幕上向左或向右移动图像,我希望他的图像停在屏幕边缘,但不能再移动,我不知道如何添加代码来做到这一点。
-(IBAction)Left:(id)sender{
[MenuClick play];
LeftTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(GoLeft) userInfo:nil repeats:YES];
if (Left == nil) {
LeftTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(GoLeft) userInfo:nil repeats:YES];
}
}
-(IBAction)StopLeft:(id)sender{
[LeftTimer invalidate];
LeftTimer = nil;
}
-(IBAction)Right:(id)sender{
[MenuClick play];
RightTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(GoRight) userInfo:nil repeats:YES];
if (Right == nil) {
RightTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(GoRight) userInfo:nil repeats:YES];
}
}
-(IBAction)StopRight:(id)sender{
[RightTimer invalidate];
RightTimer = nil;
}
-(void)GoLeft{
Ship.center = CGPointMake(Ship.center.x -5, Ship.center.y);
}
-(void)GoRight{
Ship.center = CGPointMake(Ship.center.x +5, Ship.center.y);
}