UIdemo 制作一个简单的iPhone相册

主要代码


RootView.h

#import <UIKit/UIKit.h>

@interface RootView : UIView

//定义大的 scrollView---它的上面需要放置8个 ImageScrollView
@property(nonatomic,strong)UIScrollView *mainScrollView;

//定义一个数组(存放8个 ImageScrollView)
@property(nonatomic,strong)NSMutableArray *array;


@end


RootView.m

#import "RootView.h"
#import "ImageScrollView.h"
#define k_screenWidth [UIScreen mainScreen].bounds.size.width
#define k_screenHeight [UIScreen mainScreen].bounds.size.height

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMainScrollView];
        [self createEightImageScrollView];
    }
    return self;
}

#pragma mark 创建mainScrollView
-(void)createMainScrollView{
    
    self.mainScrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //设置 contentSize
    self.mainScrollView.contentSize = CGSizeMake(8 * k_screenWidth, 0);
    //整页翻动
    self.mainScrollView.pagingEnabled = YES;
    [self addSubview:self.mainScrollView];
    

}

#pragma mark 创建8个小的 imageScrollView
-(void)createEightImageScrollView{
    
    for (int i = 0; i < 8; i++) {
        //图片的名字 --注意 i+1
        NSString *name = [NSString stringWithFormat:@"h%d.jpeg",i + 1];
        
        ImageScrollView *smallScroll = [[ImageScrollView alloc]initWithFrame:CGRectMake(i * k_screenWidth, 0, k_screenWidth, k_screenHeight) withImageName:name];
        
        //设置最大的放大比例
        smallScroll.maximumZoomScale = 2.0f;
        //设置最小的缩放比例
        smallScroll.minimumZoomScale = 0.5f;
        
        
        
        [self.mainScrollView addSubview:smallScroll];
        //不可以使用_array
        [self.array addObject:smallScroll];
        
        
    }
}

#pragma mark 重写 @property(nonatomic,strong)NSMutableArray *array 的   getter 方法

// 懒加载 ---可以在一定程度上缓解内存问题(什么时候使用,什么时候创建)
-(NSMutableArray *)array{
    
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}





@end


RootViewController.m

//
//  RootViewController.m
//  UI_lesson7_photo
//
//  Created by lanou3g on 15/10/30.
//  Copyright (c) 2015年 lirui. All rights reserved.
//

#import "RootViewController.h"
#import "RootView.h"
#import "ImageScrollView.h"

@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)RootView *rootView;
//用来记录,被缩放的小ImageScrollView
@property(nonatomic,strong)ImageScrollView *selectScroll;
@end

@implementation RootViewController

-(void)loadView{
    self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置 mainScrollView 的 代理
    self.rootView.mainScrollView.delegate = self;
    //设置 8个小的 imageScrollView 的代理
    for (ImageScrollView *temp in self.rootView.array) {
        temp.delegate = self;
    }
    
    //缩放功能的实现 (UIScrollView 自带的缩放功能)
    //1.需要通过属性设置放大和缩小的 倍数
    //2.通过代理告诉操作系统,你需要缩放的控件 (UIScrollView 上面的控件)
    
}

#pragma mark 缩放的控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    if (scrollView != self.rootView.mainScrollView) {
        ImageScrollView *imageScroll = (ImageScrollView *)scrollView;
        return imageScroll.displayImageView;
    }
    return nil;
}

#pragma mark 结束减速的时候
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.rootView.mainScrollView) {
        NSInteger index = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
        ImageScrollView *currentShowScroll = self.rootView.array[index];
        if (currentShowScroll != self.selectScroll) {
            [self.selectScroll setZoomScale:1.0f animated:YES];
            CGPoint center ;
            center.x = [UIScreen mainScreen].bounds.size.width / 2;
            center.y = [UIScreen mainScreen].bounds.size.height / 2;
            // 每次缩小之后重新设置 scrollView 的中心点
            self.selectScroll.displayImageView.center = center;
       
        }
    }
}

#pragma mark 进行缩放的时候会执行的方法(不管缩放多大的比例)
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
    if (scrollView != self.rootView.mainScrollView) {
        self.selectScroll = (ImageScrollView *)scrollView;
        //  zoomScale 默认尺度
        if(scrollView.zoomScale < 1.0){
        CGPoint center ;
        center.x = [UIScreen mainScreen].bounds.size.width / 2;
        center.y = [UIScreen mainScreen].bounds.size.height / 2;
        //每次缩小之后重新设置 图片 的中心点
        self.selectScroll.displayImageView.center = center;
        }
    }
    
}



- (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


ImageScrollView.h

#import <UIKit/UIKit.h>

@interface ImageScrollView : UIScrollView

//1.定义 UIImageView
@property(nonatomic,strong)UIImageView *displayImageView;

#pragma mark 自定义初始化方法
-(instancetype)initWithFrame:(CGRect)frame withImageName:(NSString *)name;

@end


ImageScrollView.m

#import "ImageScrollView.h"

@implementation ImageScrollView

#pragma mark 自定义初始化方法
-(instancetype)initWithFrame:(CGRect)frame withImageName:(NSString *)name{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createdisplayImageView:name];
    }
    return self;
}

#pragma mark 创建 displayImageView 的方法
-(void)createdisplayImageView:(NSString *)name  {
    
    self.displayImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:name]];
    //(*)设置 displayImageView 的 frame
    self.displayImageView.frame = self.bounds;
    [self addSubview:self.displayImageView];
    
}



@end


AppDelegate.m

//
//  AppDelegate.m
//  UI_lesson7_photo
//
//  Created by lanou3g on 15/10/30.
//  Copyright (c) 2015年 lirui. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootVC = [[RootViewController alloc]init];
    self.window.rootViewController = rootVC;
    
    
    
    
    return YES;
}

- (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

在工程里需要添加图片,图片名尽量按照规律顺序命名,方便使用循环加载,此demo添加了8张图片,h1.jpeg---h8.jpeg

实现了 放大,缩小,翻页等基本效果,照片展示与屏幕等大。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值