iOS 自定義瀑布流相冊控件
該控件為自定義的實現瀑布流效果的控件,功能較為簡單,并提供自定義delegate以供使用 功能會一步步完善起來 注意內存問題:不顯示的image設成nil
控件分為.h 和 .m文件
效果圖
控件分為3列來顯示
-
.h文件包含協議聲明和控件聲明
@protocol UIPhotosViewDelegate <NSObject>// 當點擊某個圖片時,返回該圖片所在的UIImageView,參數即返回的imageview
- (void)clickedWithView:(UIImageView *)view;
@end
@interface UIPhotosView : UIView <UIPhotosViewDelegate> {
}
@property (nonatomic, assign) id<UIPhotosViewDelegate> delegate; // 自定義協議
@property (nonatomic, retain) UIView leftView; // 左側視圖框 @property (nonatomic, retain) UIView midView; // 中間視圖框 @property (nonatomic, retain) UIView *rightView; // 右側視圖框
@property (nonatomic, assign) CGFloat subWidth; // 視圖框的寬度,即1/3
- (void)addImage:(UIImage *)image; // 向控件加入一張圖片
@end</pre>
2. .m文件包含控件實現
@implementation UIPhotosView// 初始化
(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {
_subWidth = frame.size.width / 3.0; _leftView = [[UIView alloc] init]; _leftView.frame = CGRectMake(0, 0, _subWidth, 1); [self addSubview:_leftView]; _midView = [[UIView alloc] init]; _midView.frame = CGRectMake(_subWidth, 0, _subWidth, 1); [self addSubview:_midView]; _rightView = [[UIView alloc] init]; _rightView.frame = CGRectMake(_subWidth*2, 0, _subWidth, 1); [self addSubview:_rightView];
} return self; }
// 加入圖片
(void)addImage:(UIImage *)image { CGFloat leftHeight = _leftView.frame.size.height; CGFloat midHeight = _midView.frame.size.height; CGFloat rightHeight = _rightView.frame.size.height;
UIImageView *view = [self createImageView:image]; // 創建imageview
CGFloat wid = _subWidth; CGFloat hei = image.size.height / (image.size.width / _subWidth);
// 判斷加入最短的view內 if (leftHeight <= midHeight && leftHeight <= rightHeight) {
[_leftView addSubview:view]; view.frame = CGRectMake(0, leftHeight, wid, hei); leftHeight = leftHeight + hei; _leftView.frame = CGRectMake(0, 0, wid, leftHeight);
} else if (midHeight <= rightHeight) {
[_midView addSubview:view]; view.frame = CGRectMake(0, midHeight, wid, hei); midHeight = midHeight + hei; _midView.frame = CGRectMake(_subWidth, 0, wid, midHeight);
} else {
[_rightView addSubview:view]; view.frame = CGRectMake(0, rightHeight, wid, hei); rightHeight = rightHeight + hei; _rightView.frame = CGRectMake(_subWidth*2, 0, wid, rightHeight);
}
// 調整當前控件的frame CGFloat maxnum = [self max:leftHeight and:midHeight and:rightHeight]; self.frame = CGRectMake(0, 0, self.frame.size.width, maxnum); }
// 創建imageview 并綁定事件
(UIImageView )createImageView:(UIImage )image { UIImageView *view = [[UIImageView alloc] initWithImage:image]; view.layer.borderWidth = 1.0; view.layer.borderColor = [UIColor redColor].CGColor;
view.userInteractionEnabled = YES; UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init]; [recognizer addTarget:self action:@selector(handleClicked:)]; [view addGestureRecognizer:recognizer];
return view; }
// 點擊view時獲取imageview,傳給delegate
(void)handleClicked:(UITapGestureRecognizer )recognizer { UIImageView view = recognizer.view; [self.delegate clickedWithView:view]; }
(CGFloat)max:(CGFloat)n1 and:(CGFloat)n2 and:(CGFloat)n3 { CGFloat maxnum = n1 > n2 ? n1 : n2; maxnum = maxnum > n3 ? maxnum : n3; return maxnum; }
@end</pre>