1

How to know that a UIView is resizing and the frame is increasing or decreasing (or inner side or outer side)?

For e.g. I have a UIImageView (and I am using a third party library to resize an object). Its current frame is, (someX, someY, 200,50), now if I resize in a way it changes the width to 300 and in another case it changes it to 150. I should able to know that, its increased/decreased.

Hemang
  • 26,840
  • 19
  • 119
  • 186

2 Answers2

0

You can do some Key-Value Observing. For example, if your UIImageView *imageView then you can:

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

You also need to implement this method in order to react to the changes:

- (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
}
user212514
  • 3,110
  • 1
  • 15
  • 11
0

I was able to do it in this way,

Added a custom delegate in above library (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;
    }
}

Make a small change in this touch event in SPUserResizableView.m class.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    
    if(self.delegate && [self.delegate respondsToSelector:@selector(userResizableViewIsResizing:)]) {
        [self.delegate userResizableViewIsResizing:self];
    }
}
Hemang
  • 26,840
  • 19
  • 119
  • 186