【问题标题】:how dragged image with a min and max value?如何用最小值和最大值拖动图像?
【发布时间】:2015-07-13 19:01:07
【问题描述】:

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

- (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);
    }
}

我想为 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);
        }
    }

但每次将图像拖动到:self.image.frame.size.height =100 或 300 后,我都无法调整图像大小。他需要随时可以拖动! 谢谢!

编辑: 当 heightImage 为 100 时,我不想调整图像大小。

我的原始图像高度是 200。我的用户可以单击并调整图像大小,但不能超过 300 和小于 100。我只是设置了最小和最大高度。我希望你现在明白了。

【问题讨论】:

  • "但是每次 self.image.frame.size.height =100 或 300 时,我都无法调整图像大小。[…] 编辑:当 heightImage 为 100 时,我不想调整图像大小。” ???
  • 只是一个例子。图像每次都需要可拖动,但具有最小和最大尺寸(值)。例如,将高度增加到 300 后,我无法在 200 处调整大小。

标签: ios objective-c uiimageview draggable


【解决方案1】:

您是否尝试过测试&lt;=&gt;=

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

【讨论】:

  • 编辑:当 heightImage 为 100 时,我不想调整图像大小。我的原始图像高度是 200。我的用户可以单击并调整图像大小,但不能超过 300 且小于 100。我只是放了一个最小和最大高度。我希望你现在明白了。
【解决方案2】:

我解决了这个问题。

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);

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多