Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 照片浏覽scrollview and pageCotrol

照片浏覽scrollview and pageCotrol

編輯:關於Android編程


#import

@interface AppDelegate : UIResponder
{
NSMutableArray *_mutArrImg;
UIPageControl *_pageControl;
UIScrollView *_scrollView;
}

@property (strong, nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

_scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:_scrollView];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(320*4, self.window.frame.size.height);
_scrollView.pagingEnabled = YES;

_mutArrImg = [NSMutableArray arrayWithCapacity:0];

for (int i=0; i<4; i++) {
NSURL *url = [NSURL URLWithString:@"http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData];
dispatch_sync(dispatch_get_main_queue(), ^{
[_mutArrImg addObject:img];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, self.window.frame.size.height)];
imgView.image = img;
[_scrollView addSubview:imgView];
});
});
}

_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 30)];
_pageControl.numberOfPages = 4;
_pageControl.currentPage = 0;
_pageControl.pageIndicatorTintColor = [UIColor redColor];
[self.window addSubview:_pageControl];
[_pageControl addTarget:self action:@selector(actionCotrolPage:) forControlEvents:UIControlEventValueChanged];

[self.window makeKeyAndVisible];
return YES;
}

- (IBAction)actionCotrolPage:(id)sender
{
[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * 320, 0) animated:YES];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_pageControl.currentPage = _scrollView.contentOffset.x/320;
}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved