需求点:
1,单向滚动图
2,倒计时在图片加载完成后开始
4,网络请求图片,全部图片请求完毕后是网络加载完毕
5,图片要求等比例放大,topleft
ight与容器对其
存在问题的地方:
系统UIImageView图片的添加方式中:等比例放大模式下,图片只能居中对齐,内切。
其他的添加模式中也达不到需要的效果。
设计方案:
ScrollView做外在容器,打开分页,contentSize根据图片数量计算。
自定义HDImageView,继承自UIView,用于布局占位,内部添加UIImageView,ImageView的size根据下载的image的长宽比 及 容器的长宽比计算出来
实现代码如下:
- (void)customLayoutSubviews{
dispatch_async(dispatch_get_main_queue(), ^{
// 根据图片的长宽比 设置内部imageView 的frame
CGSize imageSize = self.image.size;
CGSize containerSize = self.frame.size;
CGFloat imageHeightWithScale = imageSize.height/imageSize.width;
CGFloat innerImageViewWith;
CGFloat innerImageViewHeight;
// 图片比容器细长
if (imageHeightWithScale > containerSize.height/containerSize.width) {
// 保持宽度与容器一致
innerImageViewWith = containerSize.width;
innerImageViewHeight = innerImageViewWith * imageHeightWithScale;
}else{
// 保持高度与容器一致
innerImageViewHeight = containerSize.height;
innerImageViewWith = innerImageViewHeight/imageHeightWithScale;
}
self.innerImageView.frame = CGRectMake(0, 0, innerImageViewWith, innerImageViewHeight);
});
}
dispatch_async(dispatch_get_main_queue(), ^{
// 根据图片的长宽比 设置内部imageView 的frame
CGSize imageSize = self.image.size;
CGSize containerSize = self.frame.size;
CGFloat imageHeightWithScale = imageSize.height/imageSize.width;
CGFloat innerImageViewWith;
CGFloat innerImageViewHeight;
// 图片比容器细长
if (imageHeightWithScale > containerSize.height/containerSize.width) {
// 保持宽度与容器一致
innerImageViewWith = containerSize.width;
innerImageViewHeight = innerImageViewWith * imageHeightWithScale;
}else{
// 保持高度与容器一致
innerImageViewHeight = containerSize.height;
innerImageViewWith = innerImageViewHeight/imageHeightWithScale;
}
self.innerImageView.frame = CGRectMake(0, 0, innerImageViewWith, innerImageViewHeight);
});
}