IOS 圖片切換展示的實現,ios圖片切換展示
先看看我們的效果
這種圖片切換經常出現商城或者論壇的首頁用於展示分析
這裡我用了UIScrollView和pageControll實現了一種平鋪導航,涉及到兩種操作:1 點擊pageControl換頁2 換頁轉換pageControll
實現
#import "ViewController.h"#define WEIGHT [UIScreen mainScreen].bounds.size.width#define HEIGHT [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UIScrollViewDelegate>@end@implementation ViewController@synthesize topDisplayScroller;@synthesize page;- (void)viewDidLoad { [super viewDidLoad]; [self createTopIndex];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}#pragma mark- 建立首頁上方滑動視圖/* * 原理:建立一個分page的,scrollView , 在建立一個浮在上面的pageController————滑動展示 */-(void)createTopIndex{ topDisplayScroller.contentSize=CGSizeMake(WEIGHT*5, topDisplayScroller.frame.size.height); topDisplayScroller.pagingEnabled=YES;//允許分頁滑動 for (int i=0; i<5; i++) { UIImageView *ImagePage=[[UIImageView alloc]initWithFrame:CGRectMake(WEIGHT*i, 0, WEIGHT, topDisplayScroller.frame.size.height)]; ImagePage.image=[UIImage imageNamed:@"1"]; [topDisplayScroller addSubview:ImagePage]; } /* * 註明:添加pageControl,但是不能添加到scroller的內容視圖上,為啥呢?因為添加到上面去以後pageControl會隨之滑動 */ page=[[UIPageControl alloc]initWithFrame:CGRectMake(WEIGHT/6, 20+topDisplayScroller.frame.size.height*2/3, 50, 20)]; page.numberOfPages=5; page.currentPageIndicatorTintColor=[UIColor orangeColor]; page.pageIndicatorTintColor=[UIColor whiteColor]; [self.page addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:page];}/* * 功能:滑動scroller,帶動pageControl */-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGPoint offset=scrollView.contentOffset; self.page.currentPage=offset.x/WEIGHT;}/* * 功能:點擊pageControll,動畫 */- (void)changePage:(id)sender{ [UIView animateWithDuration:0.3f animations:^{ NSInteger whichPage = self.page.currentPage; topDisplayScroller.contentOffset = CGPointMake(WEIGHT* whichPage, 0.0f); }];}@end
原始碼:https://git.oschina.net/zhengaoxing/TopIndex
本文原創,轉載請註明出處:http://blog.csdn.net/zhenggaoxing/article/details/43965373