前言
我又來了,今天給大家帶來的是,請大家看標題.對了,就是要做一個以TableView的分區爲頭部的一個展開視圖控制器.可能就會有人問了,這麼簡單的控件,爲甚還有寫一篇博客呢?這也是實屬沒辦法的事情,人在江湖身不由己嘛,好了現在也不多說了直接上代碼,上面都有註釋的很簡單的(大神看到的話,可以指導一下小弟)
一、關鍵代碼
需要注意點:展開時需要標記位進行判斷
@interface DWEHeaderFooterView : UITableViewHeaderFooterView
/**標記是否打開*/
@property (nonatomic, assign , getter=isOpenUp) BOOL openUp;
/**標題*/
@property (nonatomic, strong) UILabel *titleLabel;
/**分區*/
@property (nonatomic,assign) NSInteger section;
/**點擊block*/
@property (nonatomic, copy) void(^didTouchHeaderViewBlock)(DWEHeaderFooterView *aHeaderView);
@end
@interface DWEHeaderFooterView()
/**旋轉圖片*/
@property (nonatomic, strong) UIImageView *imageView;
/**標題背景*/
@property (nonatomic, strong) UIView *titleBgView;
@end
@implementation DWEHeaderFooterView
@synthesize openUp = _openUp;
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self)
{
//背景view
UIView *bgView = [[UIView alloc]initWithFrame:CGRectZero];
bgView.backgroundColor = [UIColor whiteColor];
_titleBgView = bgView;
[self.contentView addSubview:bgView];
//表頭
_titleLabel = [[UILabel alloc]init];
_titleLabel.font = [UIFont systemFontOfSize:15];
_titleLabel.textColor = [UIColor blackColor];
[_titleLabel sizeToFit];
[self.contentView addSubview:_titleLabel];
//旋轉圖片
UIImage *image = [UIImage imageNamed:@"icon_Folding"];
_imageView = [[UIImageView alloc]initWithImage:image];
[self.contentView addSubview:_imageView];
}
return self;
}
#pragma mark - SET GET
- (BOOL)isOpenUp
{
return _openUp;
}
- (void)setOpenUp:(BOOL)openUp
{
_openUp = openUp;
if (_openUp)
{
self.imageView.transform = CGAffineTransformMakeRotation(-M_PI);
}
else
{
self.imageView.transform = CGAffineTransformIdentity;
}
}
#pragma mark - Event
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
__weak __typeof(self) ws = self; __strong __typeof(ws) ss = ws;
if (_openUp)
{
[UIView animateWithDuration:0.2 animations:^{
ss.imageView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
_openUp = !_openUp;
if (_didTouchHeaderViewBlock) _didTouchHeaderViewBlock(ws);
}];
}
else
{
[UIView animateWithDuration:0.2 animations:^{
ss.imageView.transform = CGAffineTransformMakeRotation(-M_PI);
} completion:^(BOOL finished) {
_openUp = !_openUp;
if (_didTouchHeaderViewBlock) _didTouchHeaderViewBlock(ws);
}];
}
}
#pragma mark - layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
_imageView.frame = CGRectMake(width - 42, height*0.5 - 11, 22, 22);
_titleBgView.frame = CGRectMake(0, 1, width, height -2);
_titleLabel.frame = CGRectMake(25, height*0.5 - 10, width- 140, 20);
}
@end
二、項目鏈接:
休閒咖-2千萬人的技能分享平臺,人人都可以參與,適合每一種職業.
分享是一種生活,也是一種收穫. 分享技能,傳遞價值,改變世界,從我開始。我們期待你的加入。
安卓app下載鏈接:
http://sj.qq.com/myapp/detail.htm?apkName=com.alligator.xiuxianba
蘋果app下載鏈接:
https://itunes.apple.com/cn/app/xiu-xian-ka/id1160649870?mt=8