ios - 为什么 subview 不会保留在父框架中?

标签 ios objective-c cocoa-touch

我有一个 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/

相关文章:

ios - 强制其他应用在 iOS 中释放麦克风

objective-c - 从 Storyboard 访问按钮

ios - UIScrollView 总是跳回顶部

ios - UIAlertView 转换不适用于 iOS7

iphone - 为什么是 "present modal view controller"?

iphone - CALayer 是否导出其内容?

ios - iAD - adBannerView 旋转

ios - 从 NSData 解码 MP3 文件

ios - 获取 Facebook 好友返回空

iphone - 在 UITableViewCell 的 subview 上添加单击和双击手势