IOS学习—控件的综合练习

//
//  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];
    
//    要访问项目中的所有资源用mainBundle
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"imageMessages" ofType:@"plist"];
    
//    读取文件要传入绝对路径
    _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即为不保留小数位,四舍五入
//    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 注意这里的写法,如果信息处理不同的话,多用这种方式重构代码
-(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
技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。