• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
麻麻的
博客园    首页    新随笔    联系   管理    订阅  订阅

UIPageControl UIScrollView 相关代码 (今日所学)

/*

 Erica Sadun, http://ericasadun.com

 iPhone Developer's Cookbook, 3.0 Edition

 BSD License, Use at your own risk

 */

 

#import <UIKit/UIKit.h>

 

#define COOKBOOK_PURPLE_COLOR[UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]

#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

#define RSTRING(X) NSStringFromCGRect(X)

 

#define BASEHEIGHT284.0f

#define NPAGES3

 

@interface TestBedViewController : UIViewController <UIScrollViewDelegate>

{

IBOutlet UIPageControl *pageControl;

UIScrollView *sv;

}

@end

 

@implementation TestBedViewController

- (void) pageTurn: (UIPageControl *) aPageControl

{

int whichPage = aPageControl.currentPage;

[UIViewbeginAnimations:nilcontext:NULL];

[UIViewsetAnimationDuration:0.3f];

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

sv.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);

[UIViewcommitAnimations];

}

 

- (void) scrollViewDidScroll: (UIScrollView *) aScrollView

{

CGPoint offset = aScrollView.contentOffset;

pageControl.currentPage = offset.x / 320.0f;

}

 

- (void) viewDidLoad

{

self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;

self.title = @"Image Scroller";

 

// Create the scroll view and set its content size and delegate

sv = [[[UIScrollViewalloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];

sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);

sv.pagingEnabled = YES;

sv.delegate = self;

 

// Load in all the pages

for (int i = 0; i < NPAGES; i++)

{

NSString *filename = [NSStringstringWithFormat:@"image%d.png", i+1];

UIImageView *iv = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:filename]];

iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);

[svaddSubview:iv];

[iv release];

}

 

[self.viewaddSubview:sv];

 

pageControl.numberOfPages = 3;

pageControl.currentPage = 0;

[pageControl addTarget:selfaction:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

}

 

pageControl  设置颜色 如下 

 

[[UIPageControlappearance] setCurrentPageIndicatorTintColor:[UIColorcolorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]];

        //未选中状态颜色

        [[UIPageControlappearance] setPageIndicatorTintColor:[UIColorgrayColor]];

 

 

 

posted on 2013-11-12 21:01  麻麻的  阅读(215)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3