如何用最小值和最大值拖动图像?

时间:2022-05-12 20:13:12

I have this code for increase height of a image.

我有这个代码来增加图像的高度。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    touchStart = [[touches anyObject] locationInView:self.image];
    touchStart2 = [[touches anyObject] locationInView:self.tableView];
    isResizingLR = ( self.image.bounds.size.height - touchStart.y < kResizeThumbSize );
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.image];
    CGPoint previous=[[touches anyObject]previousLocationInView:self.image];

    float  deltaWidth = touchPoint.x-previous.x;
    float  deltaHeight = touchPoint.y-previous.y;


    if (isResizingLR) {

        self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, touchPoint.y + deltaWidth);
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x ,self.tableView.frame.origin.y + deltaHeight,  self.tableView.frame.size.width, self.tableView.frame.size.height - deltaHeight );
        self.viewTitle.frame = CGRectMake(self.viewTitle.frame.origin.x ,self.viewTitle.frame.origin.y + deltaHeight,  self.viewTitle.frame.size.width, self.viewTitle.frame.size.height);
    }
}

I want put a condition for a minimHeight =100 and maximHeight =300; How can i do that? I already try this :

我想为minimHeight = 100和maximHeight = 300设置条件;我怎样才能做到这一点?我已经尝试过了:

if (isResizingLR) {

        if (self.image.frame.size.height >100 && self.image.frame.size.height <300) {
        self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, touchPoint.y + deltaWidth);
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x ,self.tableView.frame.origin.y + deltaHeight,  self.tableView.frame.size.width, self.tableView.frame.size.height - deltaHeight );
        self.viewTitle.frame = CGRectMake(self.viewTitle.frame.origin.x ,self.viewTitle.frame.origin.y + deltaHeight,  self.viewTitle.frame.size.width, self.viewTitle.frame.size.height);
        }
    }

but every time after dragged image at: self.image.frame.size.height =100 or 300, i can't resize image. He need to be draggable all time! Thank you!

但每次拖动图像后:self.image.frame.size.height = 100或300,我无法调整图像大小。他需要一直拖延!谢谢!

EDIT: I don't want resize image when heightImage is 100.

编辑:我不想在heightImage为100时调整大小。

My original height of image is 200. My user can click and resize image how he want, but no over 300 and less than 100. I just put a minimum and maximum height. I hope you understand now.

我原来的图像高度是200.我的用户可以按照他想要的方式点击并调整图像大小,但不超过300且小于100.我只是设置了最小和最大高度。我希望你现在明白。

2 个解决方案

#1


Have you try to test <= and >=?

你试着测试<=和> =?

if (self.image.frame.size.height >= 100 && self.image.frame.size.height <= 300) {
    //...
}

#2


I solved the problem.

我解决了这个问题。

int aux=touchPoint.y + deltaWidth;
if (aux>300) {
            aux=300;
        }
        else if(aux<100){
            aux=100;
        }
 self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, aux);

#1


Have you try to test <= and >=?

你试着测试<=和> =?

if (self.image.frame.size.height >= 100 && self.image.frame.size.height <= 300) {
    //...
}

#2


I solved the problem.

我解决了这个问题。

int aux=touchPoint.y + deltaWidth;
if (aux>300) {
            aux=300;
        }
        else if(aux<100){
            aux=100;
        }
 self.image.frame = CGRectMake(self.image.frame.origin.x, self.image.frame.origin.y, self.image.frame.size.width, aux);