您的位置:首页 > 产品设计 > UI/UE

[置顶] iOS初级开发之封装头部视图带图片旋转<继承UITableViewHeaderFooterView>

2016-11-07 18:12 519 查看

前言

我又来了,今天给大家带来的是,请大家看标题.对了,就是要做一个以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:
9463
(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: