|
#import "CorePullScaleImageView.h"
#define iOS8 ([UIDevice currentDevice].systemVersion.floatValue>=8.0f)
static NSString *const CorePullScaleContentOffset = @"contentOffset";
@interface CorePullScaleImageView (){
UIEdgeInsets _originalContentInset;
CGFloat _navBarH;
}
@property (nonatomic, weak) UIScrollView *scrollView;
@property(nonatomic, strong)UIImageView *backView;
@end
@implementation CorePullScaleImageView
- (instancetype)init
{
self = [super init];
if (self) {
self.userInteractionEnabled = YES;
[self pullScaleImageViewPrepare];
}
return self;
}
- (UIImageView*)backView {
if (!_backView) {//【需求】话题卡片视觉调整(BUG:4341)
_backView = [[UIImageView alloc]initWithFrame:self.bounds];
// _backView.backgroundColor = [UIColor colorWithHexString:@"0x1b3053"];
_backView.image = Image(@"topic/topYaAn.png");
// _backView.alpha = 0.4;
_backView.autoresizingMask = AutoresizingFull;
}
return _backView;
}
-(void)pullScaleImageViewPrepare{
self.imageView.contentMode=UIViewContentModeScaleAspectFill;
self.clipsToBounds=YES;
[self addSubview:self.backView];
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
[super willMoveToSuperview:newSuperview];
[self.superview removeObserver:self forKeyPath:CorePullScaleContentOffset context:nil];
if (newSuperview) {
[newSuperview addObserver:self forKeyPath:CorePullScaleContentOffset options:NSKeyValueObservingOptionNew context:nil];
_scrollView = (UIScrollView *)newSuperview;
_scrollView.alwaysBounceVertical = YES;
if(_hasNavBar) _navBarH=64.0f;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (self.alpha <= 0.01 || self.hidden) return;
if ([CorePullScaleContentOffset isEqualToString:keyPath]) {
[self adjustStateWithContentOffset];
}
}
- (void)adjustStateWithContentOffset
{
CGFloat y=_scrollView.contentOffset.y + _originalHeight + _navBarH;
if(y>=0) {
return;
}
CGRect frame=self.frame;
frame.origin.y=-_originalHeight + y ;
frame.size.height=_originalHeight - y;
self.frame=frame;
self.backView.frame = self.bounds;
}
-(void)didMoveToSuperview{
[super didMoveToSuperview];
CGFloat h=_originalHeight;
CGFloat y=-h;
CGFloat w=1230/2;
CGRect frame=CGRectMake(CGRectGetWidth(_scrollView.bounds)/2-1230/4, y, w, h);
self.frame=frame;
self.backView.frame = self.bounds;
}
@end
|