相册

来源:互联网 发布:mac 安装包 编辑:程序博客网 时间:2024/04/29 15:15

这是本人在学习的过程中用UI做的一个小程序,发表在这里希望能对初学者有点帮助.微笑微笑微笑

首先在X-Code中新建一个工程,然后新建一个ViewController类,名字就叫 RootViewController吧

在工程里面拖入一组图片,该程序中使用的图片是16张,图片的名称是: 1.jpg ~~~~16.jpg

在AppDelegate.m文件中导入 RootViewController类的头文件 RootViewController.h 

AppDelegate.m文件中的代码如下:

////  AppDelegate.m//  相册////  Created by 马占文 on 14-6-12.//  Copyright (c) 2014年 dlpu.edu.cn. All rights reserved.//#import "AppDelegate.h"#import "RootViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        RootViewController * rootVC = [[RootViewController alloc] init];    self.window.rootViewController = rootVC;    [rootVC release];    [self.window release];    return YES;}- (void)dealloc{    [_window release];    [super dealloc];}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
AppDelegate.h中不需要改动

在RootViewController.h中添加一些属性, 并且要加上代理<UIScrollViewDelegate>, RootViewController.h文件中的内容如下:

////  RootViewController.h//  相册////  Created by 马占文 on 14-6-12.//  Copyright (c) 2014年 dlpu.edu.cn. All rights reserved.//#import <UIKit/UIKit.h>@interface RootViewController : UIViewController<UIScrollViewDelegate>@property (nonatomic, retain) UIScrollView * scrollVC;@property (nonatomic, retain) UIScrollView * scroll;@property (nonatomic, retain) UIPageControl * page;@end
RootViewController.m文件中的内容如下:
////  RootViewController.m//  相册////  Created by 马占文 on 14-6-12.//  Copyright (c) 2014年 dlpu.edu.cn. All rights reserved.//#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        /// 滚动视图    self.scrollVC = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 50, 280, 500)];    /// 设置背景颜色    self.scrollVC.backgroundColor = [UIColor grayColor];    /// 设置滚动内容的范围    self.scrollVC.contentSize = CGSizeMake(4500, 0);        /// 内容距离上左下右的边缘的距离//    self.scrollVC.contentInset = UIEdgeInsetsMake(20, 20, 0, 0);        /// 是否滑动到顶部    self.scrollVC.scrollsToTop = YES;        /// 整屏翻动    self.scrollVC.scrollEnabled = YES;        /// 是否显示滚动条    self.scrollVC.showsHorizontalScrollIndicator = YES;    self.scrollVC.showsVerticalScrollIndicator = NO;        /// 将ViewController设置为svorllview的 代理人    self.scrollVC.delegate = self;        /// 缩放代理   scrollView的缩放    /// 设置scrollView的缩放范围//    self.scrollVC.minimumZoomScale = 0.5;//    self.scrollVC.maximumZoomScale = 2;        /// 设置缩放是是否反弹    self.scrollVC.bouncesZoom = YES;    self.scrollVC.pagingEnabled = YES;        [self.view addSubview:self.scrollVC];            NSMutableArray *scrollArr = [NSMutableArray array];    for (int i = 0; i < 16; i++) {                self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * 280 + 10, 0, 260, 450)];        self.scroll.delegate = self;                UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 500)];        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i + 1]];//        [imageView setZoomScale:1.0];                [self.scroll addSubview:imageView];        [self.scrollVC addSubview:self.scroll];        /// 设置偏移量//        self.scrollVC.contentOffset = CGPointMake(0, 0);        [self.scroll release];        [imageView release];                [scrollArr addObject:self.scroll];                self.scroll.tag = 1000 + i;                self.scroll.minimumZoomScale = 0.5;        self.scroll.maximumZoomScale = 2;        self.scroll.bouncesZoom = YES;            }        [self.scrollVC release];    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 480, 280, 100)];    self.page.numberOfPages = 16;        [self.view addSubview:self.page];}/// 滚动就会触发的方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    if (scrollView == self.scrollVC) {        self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;    }    }/// 结束减速的时候- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    if (scrollView == self.scrollVC) {        for (int i = 0; i < 16; i++) {            UIScrollView *scrView = (UIScrollView *)[scrollView viewWithTag:1000+i];            [scrView setZoomScale:1];        }    }}/// 缩放的方法- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{        if (scrollView != self.scrollVC) {        return [scrollView.subviews firstObject];    }    return nil;    }- (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
此时一个简易的相册就完成了,可以实现相册中图片的滚动,放大和缩小等功能.当然有许多的不完善的地方,希望小伙伴们能够批评指正,共同交流,共同学习.

该程序还在不断地更新中... ...



1 0