如何知道UIView正在调整内侧或外侧?

时间:2016-04-22 02:49:42

标签: ios objective-c uiview resize

如何知道UIView正在调整大小并且框架正在增加或减少(或内侧或外侧)?

例如我有UIImageView(我正在使用第三方webapp2.abort()来调整对象的大小)。它的当前帧是(someX,someY,200,50),现在如果我以某种方式调整大小,它将宽度更改为300,而在另一种情况下,它将其更改为150.我应该知道,它增加/减少。

2 个答案:

答案 0 :(得分:0)

你可以做一些Key-Value Observing。例如,如果你的UIImageView *imageView,那么你可以:

[imageView addObserver:self forKeyPath:@"frame" options:0 context:NULL];

您还需要实施此方法以对更改做出反应:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    UIImageView *imageView = (UIImageView *)object;
    // Do what you gotta do, like save the current size in 
    // an instance variable so you can compare and see increase/decrease in size
}

答案 1 :(得分:0)

我能够以这种方式做到这一点,

在上面的库(SPUserResizableView.h),- (void)userResizableViewIsResizing:(SPUserResizableView *)userResizableView {}

中添加了自定义委托
- (void)userResizableViewIsResizing:(SPUserResizableView *)userResizableView {
    id contentView = userResizableView.contentView;
    if([contentView isKindOfClass:[iOTextField class]]) {
        iOTextField *textField = (iOTextField *)contentView;        
        if([self isResizingUpWithOriginalSize:textField.referenceiOProperties.originalSize currentSize:userResizableView.frame.size withiOTextField:textField]) {
            [textField increaseFont];
        } else {
            [textField decreaseFont];
        }
    }
}

- (BOOL) isResizingUpWithOriginalSize:(CGSize)original currentSize:(CGSize)currentSize withiOTextField:(iOTextField *)textField {
    CGFloat width = (currentSize.width - original.width);
    CGFloat height = (currentSize.height - original.height);
    if(width <= 0.0f && height <= 0.0f) {
        textField.lastWidth = width;
        textField.lastHeight = height;
        return NO;
    } else if(width > textField.lastWidth) {
        if(textField.lastWidth <= 0.0f) {
            textField.lastWidth = width;
            textField.lastHeight = height;
            return NO;
        }
        textField.lastWidth = width;
        textField.lastHeight = height;
        return YES;
    }  else if(height > textField.lastHeight) {
        if(textField.lastHeight <= 0.0f) {
            textField.lastWidth = width;
            textField.lastHeight = height;
            return NO;
        }
        textField.lastWidth = width;
        textField.lastHeight = height;
        return YES;
    } else {
        textField.lastWidth = width;
        textField.lastHeight = height;
        return NO;
    }
}

SPUserResizableView.m课程中对此触摸事件进行一些小改动。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    
    if(self.delegate && [self.delegate respondsToSelector:@selector(userResizableViewIsResizing:)]) {
        [self.delegate userResizableViewIsResizing:self];
    }
}