猫分享。必须精品
主要实现相似看新闻的一个界面,不用拖拽,纯代码手工写。
首先分析app能够非常easy知道他这里有两个UILabel一个UIImageView还有两个UIButton
@property (nonatomic, strong) UILabel *noLabel;//数字标签
@property (nonatomic, strong) UIImageView *iconImage;//图片控件
@property (nonatomic, strong) UILabel *descLabel;//描写叙述信息
@property (nonatomic, strong) UIButton *leftButton;//左边button
@property (nonatomic, strong) UIButton *rightButton;//右边button
接下来就是实例化每个控件要做的了,開始的时候我是直接在- (void)viewDidLoad方法中写的,后来由于学习了懒载入
设计模式(感觉跟java设计模式中的懒汉差点儿相同)优化了代码,这里就直接给出优化后的了。
懒载入设计主要就是把UI控件放到定义好的控件的get方法中实例化,这样呢能够降低代码在viewDidLoad中的上下关系。能够有效的解耦。
-(UILabel *)noLabel
{if (_noLabel == nil) {//1,序号._noLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 40)];_Alignment = NSTextAlignmentCenter;[self.view addSubview:_noLabel];}return _noLabel;
}
[self.view addSubview:_noLabel];这个是将控件挂到view上面,画好了一定要挂上,要不没人看到。
注意:*重点。在get方法里面不能写Label;千万不要用“点”语法,这样会造成get方法死循环。由于“点”语法就是调用的get方法,所以要用下划线属性名的方法得到对象(在内存这事实上是一个指针)。
-(UIImageView *)iconImage
{if(_iconImage == nil){//2,图像CGFloat imageW = 200;//图像控件的宽 CGFloat imageH = 200;//图像控件的高CGFloat imageX = (self.view.bounds.size.width - imageW)*0.5;//图像控件的x坐标位置 CGFloat imageY = Label.frame) + 20;//图像控件的y坐标位置 _iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];[self.view addSubview:_iconImage];}return _iconImage;
}
跟上一个差点儿相同,我在凝视里面都加入了
-(UILabel *)descLabel
{if(_descLabel == nil){//3。说明CGFloat descY = CGRectGetMaxY(self.iconImage.frame);_descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width, 100)];//自己主动换行_descLabel.numberOfLines = 0;//调整文本居中显示_Alignment = NSTextAlignmentCenter;[self.view addSubview:_descLabel];}return _descLabel;
}
这个是描写叙述的。多了一个自己主动换行方法 _descLabel.numberOfLines = 0;
-(UIButton *)leftButton
{if (_leftButton == nil) {//4。
左边的button
_leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; CGFloat centY = y; CGFloat centX = self.igin.x * 0.5; _ = CGPointMake(centX, centY); //设置默认情况button显示状况 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal" ] forState:UIControlStateNormal]; //设置高亮 当按下button时候显示的样子 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted" ] forState:UIControlStateHighlighted]; _leftButton.tag = -1;//设置button的tag [self.view addSubview:_leftButton]; } return _leftButton; } -(UIButton *)rightButton { if (_rightButton == nil) { //5。右边的button CGFloat centX = self.igin.x * 0.5; CGFloat centY = y; _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _ = CGPointMake(self.view.bounds.size.width - centX,centY); //设置默认情况下button [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal" ] forState:UIControlStateNormal]; //设置高亮 [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted" ] forState:UIControlStateHighlighted]; _rightButton.tag = 1; [self.view addSubview:_rightButton]; } return _rightButton;//设置button的tag }
这里设置了左右button,開始那些都不说了。看到CGFloat我们就应该能瞬间想到布局位置什么那些关键字对应的
CGRect CGSize CGPoint 以及另外三个frame bounds center
这里有个非常精妙的设计,那就是tag 把tag设置成了1和-1在后面会有妙用。
//当前显示的照片索引
@property (nonatomic, assign) int index;
//图片的集合
@property (nonatomic, strong) NSArray *imageList;
//显示图片信息
- (void) showPhotoInfo
{//给序号加入内容 从imageList数组中拿到 = [NSString stringWithFormat:@"%d/%d",self.index+1,unt];
// 给图片加入内容从imageList数组中拿到self.iconImage.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];// 给描写叙述加入内容从imageList数组中拿到 = self.imageList[self.index][@"desc"];abled = (self.index != 0);//当索引到第一张图片的时候。让左边button编程不能按的状态abled = (self.index != unt-1);//当索引到最后图片的时候。让右边边button编程不能按的状态
}- (NSArray *)imageList
{if (_imageList == nil) {//设置存放内容(plist)的路径//在oc中contentsOfFile,通常须要完整的路径NSString *path = [[NSBundle mainBundle] pathForResource:@"imageList" ofType:@"plist"];_imageList = [NSArray arrayWithContentsOfFile:path];NSLog(@"%@",_imageList);}return _imageList;
}
这里用到了_imageList = [NSArray arrayWithContentsOfFile:path];
来从我们设置好的imageList.plist文件里得到要用的东东
- (void)viewDidLoad
{[super viewDidLoad];//显示信息[self showPhotoInfo];//button点击事件调用[_leftButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];[_rightButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];
}
//点击button事件
- (void) chickButton:(UIButton *)button
{self.index += button.tag;[self showPhotoInfo];
}
这里我们用到了一个非常精妙的地方,还记得前面的tag属性吧
这里我们直接 self.index += button.tag; 然后实现了button按左边index自增右边自减从而优化代码。
[_leftButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];
[_rightButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];
}
这两个方法建立监听。恩 这样就能点击调用代码了。
好了 到这里位置就算完毕了。没用用前段拖拽吧。
转载于:.html
本文发布于:2024-02-04 22:58:21,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170718315260509.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |