一.项目要求:制作一个浏览图片的Demo,要求包含夜间模式,以及改变图片大小,能够显示不同的图片描述

ios学习-制作一个浏览图片的Demo-LMLPHP

二.开发步骤:
1、在storyboard上添加一个空白的View,然后添加”设置“按钮,添加image View,图片序号Label,图片描述Label,更改图片Slider控件。
2、编写sliderValueChanged方法
3、在storyboard再添加一个空白的View,在新增的View上面添加Switch控件,用于夜间模式,添加Slider控件,用于改变图片的大小。
4、编写setting 方法,编写nightModel方法,编写imageSizeChange方法
三.详细步骤
1.打开Xcode创建一个新工程,点击storyboard,在上面先后添加View,Button,Label,Slider控件如图:
ios学习-制作一个浏览图片的Demo-LMLPHP
2.将用到的图片放在supporting file文件中
3.添加plist文件

4.设置各个控件如图,并连线实现方法
ios学习-制作一个浏览图片的Demo-LMLPHP
5.添加夜间模式和改变图片大小的功能
 
四.代码
 
//
//  ViewController.h
//  图片浏览器
//
//  Created by yongjianyu on 15/12/1.
//  Copyright (c) 2015年 yongjianyu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UILabel *imageNum;

@property (weak, nonatomic) IBOutlet UILabel *imageDesc;

@property (weak, nonatomic) IBOutlet UIView *settingView;

- (IBAction)setting;

- (IBAction)nightModel:(UISwitch *)sender;

- (IBAction)imageSizeChange:(UISlider *)sender;

@end

  

//
//  ViewController.m
//  图片浏览器
//
//  Created by yongjianyu on 15/12/1.
//  Copyright (c) 2015年 yongjianyu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)sliderValueChanged:(UISlider *)sender {

    //1.设置中间的图片
    NSString *imageName = [NSString stringWithFormat:@"%.f.jpg",sender.value];
    _imageView.image = [UIImage imageNamed:imageName];
    //2.设置序号
    _imageNum.text = [NSString stringWithFormat:@"%.f/8",sender.value];

    //3.设置描述
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"descr" ofType:@"plist"];
//    NSLog(@"%@",path);
    NSArray *allDescs = [NSArray arrayWithContentsOfFile:path];
//    NSLog(@"%@",allDescs);
    int no = (int)sender.value - 1;
    _imageDesc.text = allDescs[no];

}
- (IBAction)setting {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    CGPoint tempCenter = _settingView.center;
    if (_settingView.frame.origin.y == self.view.frame.size.height) {
        tempCenter.y -= _settingView.frame.size.height;
    }else{
        tempCenter.y += _settingView.frame.size.height;
    }

    _settingView.center = tempCenter;
    [UIView commitAnimations];
}
- (IBAction)nightModel:(UISwitch *)sender {

    if (sender.on) {
        self.view.backgroundColor = [UIColor grayColor];
    }else{
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

- (IBAction)imageSizeChange:(UISlider *)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    _imageView.transform = CGAffineTransformMakeScale(sender.value, sender.value);
    [UIView commitAnimations];

}
@end

  

04-27 04:51