iOS UI_相册

来源:互联网 发布:java测试工程师面试题 编辑:程序博客网 时间:2024/04/19 10:40
////  MainViewController.m//  相册////  Created by dllo on 15/8/6.//  Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()//建立一个数组,存放图片@property(nonatomic,retain)NSMutableArray *arr;@end@implementation MainViewController-(void)dealloc{    [_arr release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    //相册的目录界面,先改成不透明    self.navigationController.navigationBar.translucent=NO;    self.view.backgroundColor=[UIColor orangeColor];    //设置标题    self.navigationItem.title=@"相册";    //初始化数组    self.arr = [[NSMutableArray alloc] init];    //七张视图    for (int i = 1; i < 8; i++) {        NSString *imageName=[NSString stringWithFormat:@"h%d.jpeg",i];        UIImage *image=[UIImage imageNamed:imageName];        [self.arr addObject:image];    }    //建立七个button    for (int i = 0; i < 3; i++) {        for (int j = 0; j < 3; j++) {            if (3 * i + j < 7) {                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];                button.tag=3 * i + j +1;                button.frame=CGRectMake(j * 120 + 20, i * 120 + 100, 100, 100);                [self.view addSubview:button];                button.layer.borderWidth=1;                //把图片放到对应的button                [button setImage:(UIImage *)self.arr[3 * i + j] forState:UIControlStateNormal];                [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];            }        }    }}-(void)click:(UIButton *)button{    //点击跳到下一页    SecondViewController *secVC=[[SecondViewController alloc] init];    [self.navigationController pushViewController:secVC animated:YES];    [secVC release];    //传一个number值    secVC.number=button.tag - 1;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end
////  SecondViewController.h//  相册////  Created by dllo on 15/8/6.//  Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController@property(nonatomic,assign)NSInteger number;@end
////  SecondViewController.m//  相册////  Created by dllo on 15/8/6.//  Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "SecondViewController.h"//定义两个宏,宽'高#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface SecondViewController ()<UIScrollViewDelegate>@property(nonatomic,retain)UIScrollView *scrollView;@end@implementation SecondViewController-(void)dealloc{    [_scrollView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    //创建滚动页面    self.scrollView=[[UIScrollView alloc] initWithFrame:self.view.frame];    [self.view addSubview:self.scrollView];    [_scrollView release];    //让视图滚动起来属性,滚动范围    //-100是为了下面的滚动圆点    self.scrollView.contentSize=CGSizeMake(7 * WIDTH, HEIGHT - 100);    //按页进行滚动    self.scrollView.pagingEnabled=YES;    //滚动的图片    for (int i = 1; i < 8; i++) {        NSString *imageName =[NSString stringWithFormat:@"h%d.jpeg",i];        //创建滚动的七个视图        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];        //设置图片的起始位置        imageView.frame=CGRectMake(i * WIDTH - WIDTH, 0, WIDTH, HEIGHT-100);        [self.scrollView addSubview:imageView];        [imageView release];    }    // 关闭边界回弹效果    self.scrollView.bounces=NO;    //关闭滚动条    self.scrollView.showsHorizontalScrollIndicator=NO;    self.scrollView.showsVerticalScrollIndicator=NO;    //设置代理人    self.scrollView.delegate=self;    //创建圆点    UIPageControl *page=[[UIPageControl alloc] initWithFrame:CGRectMake(100, 580, 200, 40)];    page.backgroundColor=[UIColor whiteColor];    [self.view addSubview:page];    [page release];    //圆点个数    page.numberOfPages=7;    //圆点颜色    page.pageIndicatorTintColor=[UIColor cyanColor];    //选中点的颜色    page.currentPageIndicatorTintColor=[UIColor yellowColor];    page.tag=1000;    //显示第几页在第一张    self.scrollView.contentOffset=CGPointMake(WIDTH * self.number, 0);    NSString *str=[NSString stringWithFormat:@"第%ld张",self.number+1];    self.navigationItem.title=str;}//点随着图片滚动动-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    UIPageControl *page=(UIPageControl *)[self.view viewWithTag:1000];    page.currentPage =self.scrollView.contentOffset.x / WIDTH;    NSString *str=[NSString stringWithFormat:@"第%ld张",page.currentPage + 1];    self.navigationItem.title=str;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end![这里写图片描述](http://img.blog.csdn.net/20150810083852590)

这里写图片描述

0 0