图片自动滚动

今天学习一个好玩的东西,图片自动滚动,相信很多初学者会喜欢.

第一步:创建两个类,分别继承于UIViewController and UIView .

第二步:

myScrollView.h

#import <UIKit/UIKit.h>

//协议第一步:声明协议

@protocol MyScrollViewDelegate <NSObject>


- (void)selectScrollIndex:(NSInteger)index image:(UIImageView *)imageView;


@end


@interface MyScrollView : UIView

//协议第二步:定制代理人

@property (nonatomic, assign) id<MyScrollViewDelegate>delegate;


- (void)setImages:(NSMutableArray *)imagesArray;


@end


第三步:

myScrollView.m

#import "MyScrollView.h"


@interface MyScrollView ()<UIScrollViewDelegate>


@property (nonatomic, retain) UIScrollView *scrollView;

@property (nonatomic, assign) NSInteger page;


@end


@implementation MyScrollView


- (void)dealloc

{

    [_scrollView release];

    [super dealloc];

}


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.page = 1;

        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

        _scrollView.delegate = self;

        //边框回弹取消

//        _scrollView.bounces = NO;

        [self addSubview:_scrollView];

        [_scrollView release];

    }

    return self;

}


- (void)setImages:(NSMutableArray *)imagesArray

{

    [imagesArray insertObject:[imagesArray lastObject] atIndex:0];

    [imagesArray addObject:[imagesArray objectAtIndex:1]];

    

    [self.scrollView setContentSize:CGSizeMake(self.frame.size.width * [imagesArray count], 0)];

    [self.scrollView setPagingEnabled:YES];

    

    for (int i = 0; i < [imagesArray count]; i++) {

        NSString *path = [[NSBundle mainBundle] pathForResource:[imagesArray objectAtIndex:i] ofType:@"png"];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];

        [imageView setFrame:CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];

        [imageView setUserInteractionEnabled:YES];

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];

        [imageView addGestureRecognizer:tapGesture];

        [tapGesture release];

        [self.scrollView addSubview:imageView];

        [imageView release];

    }

    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];

    // 计时器,每隔几秒做一个事件

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

}


// 自动循环滚动

- (void)timerAction:(NSTimer *)timre

{

    self.page = _scrollView.contentOffset.x / _scrollView.frame.size.width;

    self.page++;

    CGFloat offWidth = self.page * self.scrollView.frame.size.width;

    [UIView animateWithDuration:0.5 animations:^{

        [self.scrollView setContentOffset:CGPointMake(offWidth, 0)];

    }];

    if (_page == _scrollView.contentSize.width / _scrollView.frame.size.width - 1) {

        _page = 1;

        [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];

    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{


    if (scrollView.contentOffset.x == 0) {

        [scrollView setContentOffset:CGPointMake(scrollView.contentSize.width - scrollView.frame.size.width * 2, 0)];

    } if (scrollView.contentOffset.x == scrollView.contentSize.width - scrollView.frame.size.width) {

        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)];

    }

}


- (void)tapGestureAction:(UITapGestureRecognizer *)tapGesture

{

    UIImageView *imageView = (UIImageView *)tapGesture.view;

    NSInteger currentPage = self.page;

    if ([self.delegate respondsToSelector:@selector(selectScrollIndex:image:)]) {

        if (currentPage == 0) {

            currentPage = _scrollView.contentSize.width / _scrollView.frame.size.width - 3; // 有用的方法

            [self.delegate selectScrollIndex:currentPage image:imageView];

            return;

        } if (currentPage == _scrollView.contentSize.width / _scrollView.frame.size.width - 1) {

            currentPage = 0;

            [self.delegate selectScrollIndex:currentPage image:imageView];

            return;

        } else {

            currentPage--;

            [self.delegate selectScrollIndex:currentPage image:imageView];

        }

    }

}


@end

第四步:
mainViewController.m

#import "MainViewController.h"

#import "MyScrollView.h"

//协议第四步:签订协议

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate, MyScrollViewDelegate>


@property (nonatomic, retain) NSMutableArray *array;


@end


@implementation MainViewController


- (void)dealloc

{

    [_array release];

    [super dealloc];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.array = [NSMutableArray array];

    }

    return self;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // 避免scrollView显示偏移

//    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationController.navigationBar.translucent = NO;

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self createMyScrollView];

}


- (void)createMyScrollView

{

    MyScrollView *myScrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 200)];

//协定第五步:设置代理人

    [myScrollView setDelegate:self];

    NSMutableArray *array = [NSMutableArray array];

    for (int i = 0; i < 5; i++) {

        [array addObject:[NSString stringWithFormat:@"00%d", i + 1]];

    }

    [myScrollView setImages:array];

    

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 64) style:UITableViewStylePlain];

    [tableView setDataSource:self];

    [tableView setDelegate:self];

    [self.view addSubview:tableView];

    [tableView release];

    

    tableView.tableHeaderView = myScrollView;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 200;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 50;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 5;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *identify = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];

    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];

    }

    return cell;

}

//协议第六步:实现方法

- (void)selectScrollIndex:(NSInteger)index image:(UIImageView *)imageView

{

    NSLog(@"%@   %ld", imageView, index);

}



第五步:

AppDelegate.m

#import "AppDelegate.h"

#import "MainViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [_window setBackgroundColor:[UIColor whiteColor]];

    [_window makeKeyAndVisible];

    [_window release];

    

    MainViewController *main = [[MainViewController alloc] init];

    UINavigationController *navi = [[UINavigationController alloc] init];

    

    [_window setRootViewController:navi];

    [navi pushViewController:main animated:YES];

    [navi release];

    [main release];

    return YES;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值