自己根據(jù)作者,虛心的學(xué)習(xí)了一番。初出校園,能力還是很欠缺谦秧。跟著作者走了一遍,也學(xué)到了一些東西撵溃。
1.layer的幾個小知識點:
view.layer.shadowColor = [UIColor blackColor].CGColor; // 陰影的顏色
view.layer.shadowOffset = CGSizeMake(0.5, 0.5); // 陰影的偏移量
view.layer.shadowOpacity= 1; // 陰影的不透明度
view.layer.shadowRadius = 5; // 陰影的圓角
view.layer.borderWidth = 1; // 層的 邊界 寬度
view.layer.borderColor = [UIColor blueColor].CGColor; // 層的 邊界的顏色
Simulator Screen Shot 2016年1月29日 下午3.03.51.png
2.初始化總實現(xiàn)步驟
- 根據(jù)字符串?dāng)?shù)組,添加底層的label
- 添加中間的拖動視圖'midView'
- 在midView上添加lableView,labelView中包含了一個全尺寸的label---和底層label一樣大小
- midView中調(diào)用來如下代碼防止多余的label展示出來
midView.clipsToBounds = YES;
代碼如下
- (instancetype)initWithFrame:(CGRect)frame andTitleArr:(NSArray *)titleArr
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
_height = self.frame.size.height / titleArr.count;
// 添加底層的label
for (int i = 0; i < titleArr.count; i++) {
[self addSubview:[self returnLabel:titleArr[i] andFrame:CGRectMake(0, i * _height, self.frame.size.width, _height) andColor:[UIColor blackColor]]];
}
_midView = ({
UIView * midView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, _height)];
midView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
midView.clipsToBounds = YES;
midView.userInteractionEnabled = YES;
midView;
});
[self addSubview:_midView];
// 未明白這種寫法 到底是什么寫法?缘挑?集歇?
_lableView = ({
UIView * labelView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _midView.frame.size.width, _midView.frame.size.height)];
for (int i = 0; i < titleArr.count; i++) {
[labelView addSubview:[self returnLabel:titleArr[i] andFrame:CGRectMake(0, i * _height, self.frame.size.width, _height) andColor:[UIColor whiteColor]]];
}
labelView;
});
[_midView addSubview:_lableView];
// 添加拖動手勢
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_midView addGestureRecognizer:pan];
}
return self;
}
3.手勢的學(xué)習(xí)
作為一個小心手真的手勢在我面前都是那么的新鮮,
- (void)pan:(UIPanGestureRecognizer*)sender
{
CGPoint pt = [sender translationInView:self];
CGPoint midCenter = _midView.center;
CGPoint labelCenter = _lableView.center;
midCenter.y += pt.y;
labelCenter.y -= pt.y;
// 邊界
if (midCenter.y > self.frame.size.height - _height/2.0) {
midCenter.y = self.frame.size.height - _height/2.0;
labelCenter.y = -self.frame.size.height + _height * 1.5;
}
if (midCenter.y < _height/2.0) {
midCenter.y = _height/2.0;
labelCenter.y = _height/2.0;
}
_midView.center = midCenter;
_lableView.center = labelCenter;
[sender setTranslation:CGPointZero inView:self];
}
這邊是label的代碼
// 創(chuàng)建label
- (UILabel *)returnLabel:(NSString*)title andFrame:(CGRect)frame andColor:(UIColor*)color
{
UILabel * label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont systemFontOfSize:20.f];
label.textColor = color;
label.text = title;
label.textAlignment = NSTextAlignmentCenter;
return label;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者