Возможно, этот метод поможет вам. Я использовал его в простой игре в понг, которую я сделал некоторое время назад. Это для UIView, это отскок для игры в понг. Я ограничил движение перекладины в направлении x, а не за пределами границ экрана.
Если что-то непонятно, напишите комментарий, и я попытаюсь объяснить.
// Method for movement of the bouncing pad. Restricted movement to x-axis inside of bounds.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint prevloc = [aTouch previousLocationInView:self];
CGRect myFrame = self.frame;
//Checking how far we have moved from the previous location
float deltaX = loc.x - prevloc.x;
//Note that we only update the x-position of the pad to prevent it from moving in the y-direction.
myFrame.origin.x += deltaX;
//Making sure that the bouncePad cannot move outside of the screen
if(myFrame.origin.x < 0){
myFrame.origin.x = 0;
} else if (myFrame.origin.x + myFrame.size.width > [UIScreen main Screen].bounds.size.width) {
myFrame.origin.x = [UIScreen mainScreen].bounds.size.width - myFrame.size.width;
}
//Setting the bouncing pad frame to the one with the updated position from the touches moved event.
[self setFrame:myFrame];
}