如何知道 UIView 正在向内侧或外侧调整大小?
How to know a UIView is resizing to inner side or outer side?
如何知道一个UIView
正在调整大小并且框架在增加或减少(或内侧或外侧)?
例如我有一个 UIImageView
(我正在使用第三方 library 来调整对象的大小)。它的当前帧是 (someX, someY, 200,50),现在如果我以某种方式调整大小,它会将宽度更改为 300,而在另一种情况下,它会将其更改为 150。我应该能够知道,它的 increased/decreased.
你可以做一些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
}
我是这样做到的,
在上面的库中添加了自定义委托 (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
class 中对这个触摸事件做一个小改动。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.delegate && [self.delegate respondsToSelector:@selector(userResizableViewIsResizing:)]) {
[self.delegate userResizableViewIsResizing:self];
}
}
如何知道一个UIView
正在调整大小并且框架在增加或减少(或内侧或外侧)?
例如我有一个 UIImageView
(我正在使用第三方 library 来调整对象的大小)。它的当前帧是 (someX, someY, 200,50),现在如果我以某种方式调整大小,它会将宽度更改为 300,而在另一种情况下,它会将其更改为 150。我应该能够知道,它的 increased/decreased.
你可以做一些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
}
我是这样做到的,
在上面的库中添加了自定义委托 (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
class 中对这个触摸事件做一个小改动。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.delegate && [self.delegate respondsToSelector:@selector(userResizableViewIsResizing:)]) {
[self.delegate userResizableViewIsResizing:self];
}
}