UIScrollView代码实现循环滚动
来源:互联网 发布:linux环境搭建 编辑:程序博客网 时间:2024/04/29 20:17
//
// ViewController.m
// demoScrollView
//
// Created by kkk on 15/3/9.
// Copyright (c) 2015年 js. All rights reserved.
//
#define WIDTH_OFF_SET 630.0
#define HEIGHT_OFF_SET 0
#define SCROLLVIEW_WIDTH 390.0
#define SCROLLVIEW_HEIGHT 80.0
#import "ViewController.h"
@interface ViewController ()
@property(strong,nonatomic)UIScrollView *scrollView;
@property(strong,nonatomic)NSMutableArray *slideImages;
@end
@implementation ViewController
@synthesize scrollView, slideImages;
#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 460
#define WIDTH_OF_IMAGE 320
#define HEIGHT_OF_IMAGE 460
#define LEFT_EDGE_OFSET 0
- (void)viewDidLoad {
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"1.PNG"];
[slideImages addObject:@"2.PNG"];
[slideImages addObject:@"3.PNG"];
//add the last image first
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
for (int i = 0;i<[slideImages count];i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
}
//add the first image at the end
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
[super viewDidLoad];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width
/ 2) / self.scrollView.frame.size.width) + 1;
//go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
} else
if (currentPage==([slideImages count]+1)) {
//如果是最后+1,也就是要开始循环的第一个
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
// ViewController.m
// demoScrollView
//
// Created by kkk on 15/3/9.
// Copyright (c) 2015年 js. All rights reserved.
//
#define WIDTH_OFF_SET 630.0
#define HEIGHT_OFF_SET 0
#define SCROLLVIEW_WIDTH 390.0
#define SCROLLVIEW_HEIGHT 80.0
#import "ViewController.h"
@interface ViewController ()
@property(strong,nonatomic)UIScrollView *scrollView;
@property(strong,nonatomic)NSMutableArray *slideImages;
@end
@implementation ViewController
@synthesize scrollView, slideImages;
#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 460
#define WIDTH_OF_IMAGE 320
#define HEIGHT_OF_IMAGE 460
#define LEFT_EDGE_OFSET 0
- (void)viewDidLoad {
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"1.PNG"];
[slideImages addObject:@"2.PNG"];
[slideImages addObject:@"3.PNG"];
//add the last image first
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
for (int i = 0;i<[slideImages count];i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
}
//add the first image at the end
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
[super viewDidLoad];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width
/ 2) / self.scrollView.frame.size.width) + 1;
if (currentPage==0)//滚到第一张图片(其实是3/5)时,使用scrollRectToVisible:方法滚到3/5的位置
{
//go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
} else
if (currentPage==([slideImages count]+1)) {
//如果是最后+1,也就是要开始循环的第一个
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
0 0
- UIScrollView代码实现循环滚动
- UIScrollView代码实现循环滚动
- UIScrollView代码实现循环滚动
- 实现UISCrollView循环滚动
- 实现UIScrollView循环滚动
- UIScrollView实现循环滚动
- 实现UIScrollView循环滚动
- iPhone开发应用中UIScrollView代码实现循环滚动
- [转]iPhone开发应用中UIScrollView代码实现循环滚动
- iOS UISCrollView循环滚动 实现
- UIScrollView实现循环滚动---快捷方式
- UIScrollView实现循环滚动(一)
- UIScrollview 无缝循环滚动实现
- 实现UIScrollView的循环滚动
- iOS UISCrollView循环滚动 实现
- UIScrollView自动滚动 循环滚动视图实现
- UIScrollView实现循环滚动和自动滚动
- UIScrollView 循环滚动,代码超简单
- Ajax jQuery Web聊天室
- 题目1521:二叉树的镜像
- 北向接口/ 南向接口
- 一个完整的Flexbox指南
- GCC和C99标准中的inline
- UIScrollView代码实现循环滚动
- POJ 1004: Financial Management
- 服务器端接收客户端通过socket传递来的对象的ClassNotFoundException异常。
- 实现文字闪烁效果
- jHeartbeat - jQuery心跳包插件
- NW336网卡驱动安装后,搜索不到无线信号
- Android_异常大全
- 来北京之后的一些想法
- centos6.4下安装mysql