您好,登錄后才能下訂單哦!
項(xiàng)目主要工作,能夠通過(guò)按鈕按進(jìn)行翻上一張和下一張對(duì)相片就行查看功能。這項(xiàng)目有點(diǎn)簡(jiǎn)單,但是是學(xué)習(xí)的必經(jīng)過(guò)程。對(duì)學(xué)習(xí)的整理和參考。
主要目的:
1)熟悉3大控件的使用方法和規(guī)則
2)熟悉3大控件的編程方法
3)熟悉ISO開(kāi)發(fā)技巧
下面是今天學(xué)習(xí)內(nèi)容的源碼,這部分源碼實(shí)現(xiàn)的有點(diǎn)羅嗦,后期會(huì)改成,后來(lái)會(huì)用plist的方式和應(yīng)用數(shù)組的方式,簡(jiǎn)化實(shí)現(xiàn)圖片顯示的方法。
#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];
/*標(biāo)簽*/
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];
/*描述內(nèi)容*/
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
總結(jié):
1)如果是幾個(gè)按鈕的話, 可以通過(guò)+-1的方式利用tag進(jìn)行,簡(jiǎn)化明了。
2)uibutton和其他的控件應(yīng)用不一樣
3)在代碼中多想下代碼重復(fù)利用怎么處理能使代碼更加簡(jiǎn)單!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。