//
// ViewController.h
// 圖片瀏覽器
//
// Created by on 15/4/14.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *imageValue;
@property (weak, nonatomic) IBOutlet UILabel *imageDesc;
@property (weak, nonatomic) IBOutlet UIView *settingView;
- (IBAction)valueChange:(UISlider *)sender;
- (IBAction)settingButton;
- (IBAction)darkModel:(UISwitch *)sender;
- (IBAction)imageSize:(UISlider *)sender;
@end
//
// ViewController.m
// 圖片瀏覽器
//
// Created by on 15/4/14.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
NSArray *_imageMess;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 要訪問項(xiàng)目中的所有資源用mainBundle
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"imageMessages" ofType:@"plist"];
// 讀取文件要傳入絕對(duì)路徑
_imageMess = [NSArray arrayWithContentsOfFile:filePath];
NSDictionary *dic = _imageMess[0];
NSString *imageName = [dic objectForKey:@"icon"];
_imageView.image = [UIImage imageNamed:imageName];
NSString *imageMessage = [dic objectForKey:@"title"];
_imageDesc.text = imageMessage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)valueChange:(UISlider *)sender {
// 這里的%.f即為不保留小數(shù)位,四舍五入
// NSString *imageName = [NSString stringWithFormat:@"%.f", sender.value];
NSDictionary *dic = _imageMess[(int)sender.value - 1];
NSString *imageName = [dic objectForKey:@"icon"];
_imageView.image = [UIImage imageNamed:imageName];
NSString *imageValue = [NSString stringWithFormat:@"%.f/%.f", sender.value, sender.maximumValue];
_imageValue.text = imageValue;
NSString *imageMessage = [dic objectForKey:@"title"];
_imageDesc.text = imageMessage;
}
#pragma mark 注意這里的寫法,如果信息處理不同的話,多用這種方式重構(gòu)代碼
-(void)animationCustom:(void (^)())block {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
block();
[UIView commitAnimations];
}
- (IBAction)settingButton {
CGFloat viewHeight = _settingView.frame.size.height;
[self animationCustom:^{
if (_settingView.frame.origin.y == self.view.frame.size.height) {
CGRect frame = _settingView.frame;
frame.origin.y -= viewHeight;
_settingView.frame = frame;
} else {
CGRect frame = _settingView.frame;
frame.origin.y += viewHeight;
_settingView.frame = frame;
}
}];
}
- (IBAction)darkModel:(UISwitch *)sender {
if (sender.on) {
self.view.backgroundColor = [UIColor darkGrayColor];
} else {
self.view.backgroundColor = [UIColor whiteColor];
}
}
- (IBAction)imageSize:(UISlider *)sender {
_imageView.transform = CGAffineTransformMakeScale(sender.value, sender.value);
}
@end
更多信息請(qǐng)查看IT技術(shù)專欄