UI06_无限滚动的相册

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

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

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

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

    [_window release];

    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];



    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *page;


@end

@implementation RootViewController
- (void)dealloc
{
    [_scrollView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    self.scrollView.backgroundColor = [UIColor lightGrayColor];

    self.scrollView.contentSize = CGSizeMake(9 * WIDTH, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    //  add the first image
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageView.image = [UIImage imageNamed:@"h7.jpeg"];
    [self.scrollView addSubview:firstImageView];
    [firstImageView release];
    firstImageView.userInteractionEnabled = YES;


    for (NSInteger i = 1; i < 8; i++) {
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
        [imageView release];
        imageView.userInteractionEnabled = YES;
    }

    //  add the last image  在最后面加上一个视图, 放在最后, 显示第一张图片
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8 * WIDTH, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:lastImageView];
    [lastImageView release];
    lastImageView.userInteractionEnabled = YES;
    //  为了能显示第一张图片, 我们需要先设置一个偏移量, 这样能直接显示罗宾的第一张图片
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);

    //  设置代理人
    self.scrollView.delegate = self;

    //  用定时器实现无限滚动
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];

    //  创建一个pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 100, 150, 30)];
    [self.view addSubview:self.page];
    [self.page release];
    self.page.backgroundColor = [UIColor cyanColor];
    self.page.numberOfPages = 7;
    //  设置未被点的颜色
    self.page.pageIndicatorTintColor = [UIColor blackColor];
    //  选中的点的颜色
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];

    //  touchupInside 和 valueChange 皆可用+
    [self.page addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

    //  想要scrollView实现缩放功能, 需要先设置最大最小缩放比例
    self.scrollView.minimumZoomScale = 0.5;
    self.scrollView.maximumZoomScale = 2;


}
#pragma mark    这个方法用来管理scrollView的缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    NSLog(@"fsaf");
    return [scrollView.subviews firstObject];
}

- (void)changePage:(UIPageControl *)page {
    //  还是设置偏移量
//    self.scrollView.contentOffset = CGPointMake(page.currentPage * WIDTH + WIDTH, 0);
    NSLog(@"%ld", page.currentPage);
    [self.scrollView setContentOffset:CGPointMake(page.currentPage * WIDTH + WIDTH, 0) animated:YES];
}


- (void)changeImage {
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x - WIDTH, 0) animated:YES];
    //  自动向右滚动,,,需在上式改为+WIDTH
//    if (self.scrollView.contentOffset.x == 8 * WIDTH) {
//        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
//    }
    //  自动向左滚动
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(WIDTH * 7, 0);
    }
}

//  滑动结束, 触发的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x == 8 * WIDTH) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
    }


    //  pageControl和scrollView进行关联, 点的移动方向和scrollView的偏移量相关
    //  无论怎么方向滑动, 当pageControl和scrollView关联的时候, 偏移量都是修改好的, 所以可以正确的滚动和显示
    self.page.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值