项目主要工作,能够通过按钮按进行翻上一张和下一张对相片就行查看功能。这项目有点简单,但是是学习的必经过程。对学习的整理和参考。
主要目的:
1)熟悉3大控件的使用方法和规则
2)熟悉3大控件的编程方法
3)熟悉ISO开发技巧
下面是今天学习内容的源码,这部分源码实现的有点罗嗦,后期会改成,后来会用plist的方式和应用数组的方式,简化实现图片显示的方法。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel *iLabel;
@property (nonatomic, strong) UIImageView *iImage;
@property (nonatomic, strong) UILabel *idesLabel;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, assign) int index;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*标签*/
self.iLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
self.iLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_iLabel];
/*显示图片*/
CGFloat Image_W = 300;
CGFloat Image_H = 300;
CGFloat Image_X = (self.view.frame.size.width - Image_W) *0.5;
CGFloat Image_Y = CGRectGetMaxY(self.iLabel.frame) + 20;
self.iImage = [[UIImageView alloc]initWithFrame:CGRectMake(Image_X, Image_Y, Image_W, Image_H)];
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"biaoqingdi"];
[self.view addSubview:_iImage];
/*显示左右按钮*/
self.leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
self.leftButton.center = CGPointMake(CGRectGetMinX(self.iImage.frame) * 0.5, (CGRectGetMinY(self.iImage.frame) + self.iImage.frame.size.height* 0.5));
[_leftButton setImage:[UIImage p_w_picpathNamed:@"left_normal"] forState:UIControlStateNormal];
[_leftButton setImage:[UIImage p_w_picpathNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
_leftButton.tag = -1;
[self.view addSubview:_leftButton];
[_leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
self.rightButton.center = CGPointMake((CGRectGetMaxX(self.iImage.frame) + self.leftButton.center.x), (CGRectGetMinY(self.iImage.frame) + self.iImage.frame.size.height* 0.5));
[_rightButton setImage:[UIImage p_w_picpathNamed:@"right_normal"] forState:UIControlStateNormal];
_rightButton.tag = 1;
[_rightButton setImage:[UIImage p_w_picpathNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
[self.view addSubview:_rightButton];
[_rightButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
/*描述内容*/
self.idesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.iImage.frame) + 30, self.view.frame.size.width, 20)];
self.idesLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_idesLabel];
[self buttonAction:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) buttonAction :(UIButton *) Button
{
NSLog(@"%s", __func__);
self.index += (int)Button.tag;
/*显示索引*/
self.iLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, 5];
switch (self.index)
{
case 0:
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"biaoqingdi"];
self.idesLabel.text = @"表情帝";
break;
case 1:
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"bingli"];
self.idesLabel.text = @"病例表";
break;
case 2:
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"chiniupa"];
self.idesLabel.text = @"吃牛排";
break;
case 3:
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"danteng"];
self.idesLabel.text = @"蛋疼";
break;
case 4:
self.iImage.p_w_picpath = [UIImage p_w_picpathNamed:@"wangba"];
self.idesLabel.text = @"王八";
break;
default:
break;
}
}
- (void) leftAction
{
[self buttonAction: _leftButton];
_leftButton.enabled = (self.index != 0);
_rightButton.enabled = (self.index != 4);
}
- (void) rightAction
{
[self buttonAction:_rightButton];
_leftButton.enabled = (self.index != 0);
_rightButton.enabled = (self.index != 4);
}
@end
总结:
1)如果是几个按钮的话, 可以通过+-1的方式利用tag进行,简化明了。
2)uibutton和其他的控件应用不一样
3)在代码中多想下代码重复利用怎么处理能使代码更加简单!