我有一个 View ,里面有一个UIImage
。图像不是静态的,如果我拖动手指(使用拖动事件),我可以移动它。问题是有时图片会移动到 UIView 框架之外。将其保持在父框架边界内的适当方法是什么?
--UIViewA
--------UIViewB
--------------UIImage
--------------UI按钮
我想将 UIImage 保留在 UIViewB 中
- (IBAction)myButtonSingleTap:(UIButton *)sender {
imDragging = YES;
[_myButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
}
- (IBAction)myButtonDragInside:(UIButton *)sender
{
[_myButton addTarget:self action:@selector(draging:withEvent:) forControlEvents: UIControlEventTouchDragInside];
}
- (void)dragBegan:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
startingTouchPoint = [touch locationInView:self.view];
}
- (void)draging:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
currentTouchPoint = [touch locationInView:self.view];
_movingPic.frame = CGRectMake(currentTouchPoint.x, currentTouchPoint.y, 28, 23);
}
最佳答案
您需要在拖动过程中检查 View 的位置。
在某些时候,您将根据用户的拖动方向等设置图像的框架......
在此期间,您应该进行逻辑检查,例如...
If new location x value is less than 0 then set new location x = 0.
If new location x value plus image width is greater than view width then set new location x = view width - image width.
等...
然后使用新位置作为图像移动到的点。
关于ios - 为什么 subview 不会保留在父框架中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460229/