你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI06_UIPageControl

iOS UI06_UIPageControl

編輯:IOS開發綜合
//
//  MainViewController.m
//  UI06_UIPageControl
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import MainViewController.h
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation MainViewController
-(void)dealloc
{
    [_scrollView release];
    [_imageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.scrollView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView release];

    self.scrollView.contentSize=CGSizeMake(13 * WIDTH, HEIGHT-100);
    self.scrollView.pagingEnabled=YES;

    for (NSInteger i=0; i<13; i++) {
        NSString *picName=[NSString stringWithFormat:@cymbal_%02ld.jpg,i];
        self.imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
        self.imageView.frame=CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT-100);
        [self.scrollView addSubview:self.imageView];
        [self.imageView release];


    }
    self.scrollView.contentOffset=CGPointMake(0, 0);
    self.scrollView.bounces=NO;
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.showsVerticalScrollIndicator=NO;
    self.scrollView.delegate=self;

    UIPageControl *page =[[UIPageControl alloc] initWithFrame:CGRectMake(100, 580, 200, 40)];
    page.backgroundColor=[UIColor redColor];
    [self.view addSubview:page];
    [page release];
    //圖片個數和點的個數相同
    page.numberOfPages =13;
    //點的背景顏色
    page.pageIndicatorTintColor=[UIColor blackColor];
    //被選中的點的顏色
    page.currentPageIndicatorTintColor=[UIColor whiteColor];
    //添加點擊方法
    [page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    page.tag=1000;

    //scrollView縮放
    //縮放的比例
    self.scrollView.maximumZoomScale=2;
    self.scrollView.minimumZoomScale=0.5;
    //原始縮放比例
    self.scrollView.zoomScale=1;

}
#pragma mark 縮放協議方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews firstObject];
}



-(void)pageAction:(UIPageControl *)page
{
    //點的個數從0開始
    NSLog(@%ld,page.currentPage);
    //觸發時,進行圖片切換
//    self.scrollView.contentOffset=CGPointMake(WIDTH * (page.currentPage), 0);
    [self.scrollView setContentOffset:CGPointMake(WIDTH * (page.currentPage), 0) animated:YES];

}

//
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //滑動圖片,讓圓點跟著一起動
    UIPageControl *page=(UIPageControl *)[self.view viewWithTag:1000];
    page.currentPage = self.scrollView.contentOffset.x / WIDTH;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved