UIScrollerView和UIPageView联合使用
ViewController.h
#import <UIKit/UIKit.h>
@inte易做图ce FourthViewController : UIViewController<UIScrollViewDelegate>
{
CGFloat screenWidth;
NSArray *AdImgArray;
int TimeNum;
BOOL Tend;
}
@property (strong, nonatomic) IBOutlet UIScrollView *AdScrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *AdPageControl;
- (IBAction)PageControlValueChange:(id)sender;
@end
ViewController.m
#import "FourthViewController.h"
@inte易做图ce FourthViewController ()
@end
@implementation FourthViewController
@synthesize AdPageControl;
@synthesize AdScrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
screenWidth = self.view.frame.size.width;
AdScrollView.scrollEnabled = YES;
AdScrollView.pagingEnabled = YES;
AdScrollView.showsHorizontalScrollIndicator = NO;
AdScrollView.showsVerticalScrollIndicator = NO;
AdScrollView.delegate = self;
[NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector(handleTimer:) userInfo:nil repeats: YES];
AdImgArray = [[NSArray alloc]initWithObjects:
@"/2013/0422/20130422082319186.png",
@"/2013/0422/20130422082319311.png",
@"http://www.baidu.com/img/baidu_sylogo1.gif",nil];
[self AdImg:AdImgArray];
[self setCurrentPage:AdPageControl.currentPage];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 5秒换图片
- (void) handleTimer: (NSTimer *) timer
{
if (TimeNum % 5 == 0)
{
if (!Tend)
{
AdPageControl.currentPage ++;
if (AdPageControl.currentPage == AdPageControl.numberOfPages - 1)
Tend = YES;
}
else
{
AdPageControl.currentPage --;
if (AdPageControl.currentPage == 0)
Tend = NO;
}
[UIView animateWithDuration:0.7 //速度0.7秒
animations:^{//修改坐标
AdScrollView.contentOffset = CGPointMake(AdPageControl.currentPage * screenWidth, 0);
}];
}
TimeNum ++;
}
#pragma mark - 下载图片
void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )
{
dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
{
NSData * data = [[NSData alloc] initWithContentsOfURL:URL] ;
UIImage * image = [[UIImage alloc] initWithData:data];
dispatch_async( dispatch_get_main_queue(), ^(void){
if( image != nil )
{
imageBlock( image );
} else {
errorBlock();
}
});
});
}
- (void)AdImg:(NSArray*)arr
{
[AdScrollView setContentSize:CGSizeMake(screenWidth * [arr count], AdScrollView.frame.size.height)];
AdPageControl.numberOfPages=[arr count];
for ( int i=0; i<[arr count]; i++) {
NSString *url=[arr objectAtIndex:i];
UIButton *img=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth * i, 0, screenWidth, AdScrollView.frame.size.height)];
[img addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
[AdScrollView addSubview:img];
UIImageFromURL( [NSURL URLWithString:url], ^( UIImage * image )
{
&
补充:综合编程 , 其他综合 ,