码迷,mamicode.com
首页 > 移动开发 > 详细

IOS学习—控件的综合练习

时间:2015-04-14 18:12:46      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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
技术分享

IOS学习—控件的综合练习

标签:

原文地址:http://my.oschina.net/are1OfBlog/blog/401478

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!