为什么subView不会停留在父框架中? [英] Why a subView won't stay in the parent frame?

查看:55
本文介绍了为什么subView不会停留在父框架中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,里面有一个 UIImage .图像不是静态的,如果我用手指拖动(使用拖动事件),我可以在图像周围移动.问题是有时图片会移到 UIView 框架之外.将其保留在父框架范围内的合适方法是什么?

I have a view and inside it there's a UIImage. The image isn't static and I can move it around if I drag my finger around (using drag events) The problem is that sometimes the picture moves outside of the UIView frame. What's the appropriate way to keep it inside the parent frame bounds?

-UIViewA

-------- UIViewB

--------UIViewB

-------------- UIImage

--------------UIImage

-------------- UIButton

--------------UIButton

我想将UIImage保留在UIViewB中

I want to keep UIImage inside 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);
}

推荐答案

在拖动过程中,您需要检查视图的位置.

You will need to check the location of the view during the dragging.

有时,您将根据用户的拖动方向等来设置图像的框架.

At some point you will be setting the frame of the image depending on the user's drag direction, etc...

在此期间,您应该进行逻辑检查,例如...

During this you should have a logic check like...

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.

等...

然后使用新位置作为将图像移动到的位置.

Then use the new location as the point to move the image to.

这篇关于为什么subView不会停留在父框架中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆