您的位置:首页 > 产品设计 > UI/UE

UI06_UIScrollView

2015-09-21 19:19 281 查看
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>

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

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

//  让scrollView进行滚动, 这个属性很重要, 只有设置了这个属性它才可以进行滚动
//  水平滚动设置width, 垂直滚动设置height
scrollView.contentSize = CGSizeMake(8 * WIDTH , 0);
//  按照页来进行滚动
scrollView.pagingEnabled = YES;
//    NSLog(@"%@", scrollView.subviews);
//  把图片放到scrollView上
for (NSInteger i = 1; i < 8; i++) {
//  拼接图片名
NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
//  把图片放到imageView上
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * WIDTH, 0, WIDTH, HEIGHT)];
imageView.image = [UIImage imageNamed:imageName];
[scrollView addSubview:imageView];
[imageView release];

}
//    NSLog(@"%@", scrollView.subviews);
//    NSLog(@"%ld", scrollView.subviews.count);
//  在第7张图片后面, 再加一个imageview, 并且显示第一张图片内容, 注意, 相应的contentsize也要进行修改
UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(7 * WIDTH, 0, WIDTH, HEIGHT)];
lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
[scrollView addSubview:lastImageView];
[lastImageView release];

//  关闭边界回弹效果
scrollView.bounces = NO;
//  把水平和垂直的滚动条关闭
//  水平
scrollView.showsHorizontalScrollIndicator = NO;
//  垂直
//    scrollView.showsVerticalScrollIndicator = NO;

scrollView.tag = 1000;

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];

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

}
#pragma mark    只要一拖拽, 一滚动, 就会触发这个协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%g", scrollView.contentOffset.x);
}
#pragma mark    当滑动结束, 稳定之后才被触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"我被触发了");
//    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + WIDTH, 0)];
//    if (scrollView.contentOffset.x == 7 * WIDTH) {
//        scrollView.contentOffset = CGPointMake(0, 0);
//    }
//
if (scrollView.contentOffset.x == 7 * WIDTH) {
scrollView.contentOffset = CGPointMake(0, 0);
}
}

#pragma mark    通过NSTimer来让scrollview进行无限滚动
- (void)changeImage{

//  通过tag找scrollView
UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:1000];
//  通过设置偏移量, 让哪一张视图显示在屏幕上, 重要
[scroll setContentOffset:CGPointMake(scroll.contentOffset.x + WIDTH, 0) animated:YES];
//  判断是否到当前的最后一张, 如果到了, 需要把偏移量设置到第一张图片位置
if (scroll.contentOffset.x == 7 * WIDTH) {
scroll.contentOffset = CGPointMake(0 * WIDTH, 0);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: