how to limit the position of a UIImageView inside a boundary range?
I got an Icon of UIImageView named IconView. I want to make sure when I touch and move this Icon, its position only change inside the boundary of another UIImageView named backgroundView.
I thought after I add the IconView as the s开发者_运维知识库ubview of backgroundView, the boundary is automatically set. But it seems to be wrong.
[backgroundView addSubview:IconView];
after this, I can still move the Icon to the outside of backgroundView. how can I set the limitation? thanks.
You try like this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(urBoundryBackgroundImage.frame, location)) {
//do your moving stuff
}
}
精彩评论