如何知道UIView
正在調整大小並且框架正在增加或減少(或內側或外側)?如何知道UIView是調整到內側還是外側?
例如,我有一個UIImageView
(我正在使用第三方library來調整對象的大小)。它的當前框架是(someX,someY,200,50),現在如果我以某種方式調整寬度,它將寬度更改爲300,在另一種情況下,它將其更改爲150.我應該知道它的增加/減少。
如何知道UIView
正在調整大小並且框架正在增加或減少(或內側或外側)?如何知道UIView是調整到內側還是外側?
例如,我有一個UIImageView
(我正在使用第三方library來調整對象的大小)。它的當前框架是(someX,someY,200,50),現在如果我以某種方式調整寬度,它將寬度更改爲300,在另一種情況下,它將其更改爲150.我應該知道它的增加/減少。
我能夠做到這樣,
添加上述庫(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];
}
}
你可以做一些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
}
可能重複http://stackoverflow.com/questions/4000664/is-there-a-uiview-resize-event – luiyezheng
@luiyezheng,是的,但我想知道他是大小是否增加或減少? – Hemang