ISO开发学习之路--第一篇--图像显示器制作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ISO开发学习之路--第一篇--图像显示器制作相关的知识,希望对你有一定的参考价值。
项目主要工作,能够通过按钮按进行翻上一张和下一张对相片就行查看功能。这项目有点简单,但是是学习的必经过程。对学习的整理和参考。
主要目的:
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.image = [UIImage imageNamed:@"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 imageNamed:@"left_normal"] forState:UIControlStateNormal];
[_leftButton setImage:[UIImage imageNamed:@"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 imageNamed:@"right_normal"] forState:UIControlStateNormal];
_rightButton.tag = 1;
[_rightButton setImage:[UIImage imageNamed:@"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.image = [UIImage imageNamed:@"biaoqingdi"];
self.idesLabel.text = @"表情帝";
break;
case 1:
self.iImage.image = [UIImage imageNamed:@"bingli"];
self.idesLabel.text = @"病例表";
break;
case 2:
self.iImage.image = [UIImage imageNamed:@"chiniupa"];
self.idesLabel.text = @"吃牛排";
break;
case 3:
self.iImage.image = [UIImage imageNamed:@"danteng"];
self.idesLabel.text = @"蛋疼";
break;
case 4:
self.iImage.image = [UIImage imageNamed:@"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)在代码中多想下代码重复利用怎么处理能使代码更加简单!
本文出自 “Lelon的IOS开发之路” 博客,请务必保留此出处http://51851851888888.blog.51cto.com/7026900/1750435
以上是关于ISO开发学习之路--第一篇--图像显示器制作的主要内容,如果未能解决你的问题,请参考以下文章