ios 学习笔记 6 之 Slideshow ad

    网页上面常用的Jquery广告控件(slideshow)是通过div+jquery来实现,ios上面实现主要通过UIPageControl+UIScrollView来结合实现,效果如下图:

源码如下:

1 #import <Foundation/Foundation.h>
2 @class SlideView;
3
4 @protocol SlideScrollDelegate <NSObject>
5
6 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage;
7
8 @end
 1 #import <UIKit/UIKit.h>
2 #import "SlideView.h"
3 #import "SlideScrollDelegate.h"
4
5 @interface SlideViewController : UIViewController<SlideScrollDelegate>
6 {
7 NSMutableArray *pages;
8 SlideView *slideshow;
9 }
10
11 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage;
12
13 @end
 1 #import "SlideViewController.h"
2 static const int PAGESIZE=5;
3
4 @implementation SlideViewController
5
6 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
7 {
8 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
9 if (self) {
10 // Custom initialization
11 }
12 return self;
13 }
14
15 - (void)didReceiveMemoryWarning
16 {
17 // Releases the view if it doesn't have a superview.
18 [super didReceiveMemoryWarning];
19
20 // Release any cached data, images, etc that aren't in use.
21 }
22
23 #pragma mark - View lifecycle
24
25
26 // Implement loadView to create a view hierarchy programmatically, without using a nib.
27 - (void)loadView
28 {
29 [super loadView];
30
31 pages=[[NSMutableArray alloc]init];
32 for (int i=0; i<PAGESIZE; i++) {
33 UIImage *background=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.yishimai.com/ios/slideshow/images/black.png"]]];
34
35 UIImage *image=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yishimai.com/ios/slideshow/images/%d.jpg",i+1]]]];
36
37 UIImageView *page=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
38 page.image=background;
39
40 UIImageView *subview=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
41 subview.image=image;
42 subview.bounds=CGRectMake(0.0, 0.0, image.size.width, image.size.height);
43
44 [page addSubview:subview];
45 [pages addObject:page];
46 }
47
48 slideshow=[[SlideView alloc]initWithFrame:self.view.frame];
49 slideshow.pages = pages;
50 slideshow.delegate = self;
51 self.view = slideshow;
52 }
53
54 -(void)dealloc
55 {
56 [slideshow release];
57 [super dealloc];
58 }
59
60
61
62 - (void)viewDidUnload
63 {
64 [super viewDidUnload];
65 }
66
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
68 {
69
70 return (interfaceOrientation == UIInterfaceOrientationPortrait);
71 }
72
73
74 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage
75 {
76 NSLog(@"当前第 %d 页\n",currentPage);
77 }
78
79 @end
 1 #import <UIKit/UIKit.h>
2 #import "SlideScrollDelegate.h"
3
4 @interface SlideView : UIView<UIScrollViewDelegate>
5 {
6 UIScrollView *scrollview;
7 UIPageControl *pageControl;
8
9 CGRect _pageRegion,_controlRegion;
10 NSMutableArray *_pages;
11 id _delegate;
12 }
13
14 -(void)layoutViews;
15 -(void)notifyPageChange;
16
17 @property(nonatomic,assign,getter = getPages) NSMutableArray *pages;
18 @property(nonatomic,assign,getter = getCurrentPage)int currentPage;
19 @property(nonatomic,assign,getter = getDelegate)id delegate;
20
21
22 @end
  1 #import "SlideView.h"
2
3 @implementation SlideView
4
5 - (id)initWithFrame:(CGRect)frame
6 {
7 self = [super initWithFrame:frame];
8 if (self) {
9 _pages=nil;
10 _pageRegion=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height-50);
11 _controlRegion=CGRectMake(frame.origin.x, frame.size.height-frame.size.height/2+50, frame.size.width,10);
12 self.delegate=nil;
13
14 scrollview=[[UIScrollView alloc]initWithFrame:_pageRegion];
15 scrollview.pagingEnabled=YES;
16 scrollview.showsHorizontalScrollIndicator=NO;
17 scrollview.delegate=self;
18
19 [self addSubview:scrollview];
20
21 pageControl=[[UIPageControl alloc]initWithFrame:_controlRegion];
22 [pageControl addTarget:self action:@selector(pageControlDidChange:) forControlEvents:UIControlEventValueChanged];
23 [self addSubview:pageControl];
24 }
25 return self;
26 }
27
28 //设置内页
29 -(void)setPages:(NSMutableArray *)pages
30 {
31 if(_pages!=nil)
32 {
33 for (int i=0; i<[_pages count]; i++) {
34 [[_pages objectAtIndex:i]removeFromSuperview];
35 }
36 }
37
38 _pages=pages;
39 scrollview.contentOffset=CGPointMake(0.0, 0.0);
40 scrollview.contentSize=CGSizeMake(_pageRegion.size.width*[_pages count], _pageRegion.size.height);
41 pageControl.numberOfPages=[_pages count];
42 pageControl.currentPage=0;
43 [self layoutViews];
44
45 }
46
47 //拖动结束改变pageView上面的点位置
48 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
49 {
50 self.currentPage=self.currentPage;
51 [self notifyPageChange];
52 }
53
54
55 -(NSMutableArray *)getPages
56 {
57 return _pages;
58 }
59
60 //加载Scrollview内容
61 -(void) layoutViews
62 {
63 for (int i=0; i<[_pages count]; i++) {
64 UIView *page=[_pages objectAtIndex:i];
65 CGRect bounds=page.bounds;
66 CGRect frame=CGRectMake(_pageRegion.size.width*i, 0.0, _pageRegion.size.width, _pageRegion.size.height);
67 page.frame = frame;
68 page.bounds=bounds;
69 [scrollview addSubview:page];
70 }
71 }
72
73 -(id)getDelegate
74 {
75 return _delegate;
76 }
77
78 -(void)setDelegate:(id)delegate
79 {
80 _delegate=delegate;
81 }
82 //设置当前页
83 -(void)setCurrentPage:(int)currentPage
84 {
85 [scrollview setContentOffset:CGPointMake(_pageRegion.size.width*currentPage, scrollview.contentOffset.y) animated:YES];
86 pageControl.currentPage=currentPage;
87 }
88 //得到当前页
89 -(int)getCurrentPage
90 {
91 return (int)(scrollview.contentOffset.x/_pageRegion.size.width);
92 }
93 //点击pageview上面的点
94 -(void)pageControlDidChange:(id) sender
95 {
96 UIPageControl *control=(UIPageControl *)sender;
97 if(control==pageControl)
98 {
99 self.currentPage=control.currentPage;
100 }
101 [self notifyPageChange];
102 }
103 //通知委托事件
104 -(void)notifyPageChange
105 {
106 if(self.delegate!=nil)
107 {
108 if([_delegate conformsToProtocol:@protocol(SlideScrollDelegate)])
109 {
110 if ([_delegate respondsToSelector:@selector(slideshowDidChangeCurrentView:CurrentPage:)]) {
111 [self.delegate slideshowDidChangeCurrentView: (SlideView *)self CurrentPage:self.currentPage];
112 }
113 }
114 }
115 }
116
117
118
119 @end
1 #import <UIKit/UIKit.h>
2 #import "SlideViewController.h"
3
4 @interface AppDelegate : UIResponder <UIApplicationDelegate>
5
6 @property (strong, nonatomic) UIWindow *window;
7 @property (strong, nonatomic) SlideViewController *viewController;
8
9 @end
 1 #import "AppDelegate.h"
2
3 @implementation AppDelegate
4
5 @synthesize window = _window;
6 @synthesize viewController=_viewController;
7
8 - (void)dealloc
9 {
10 [_viewController release];
11 [_window release];
12 [super dealloc];
13 }
14
15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
16 {
17 CGRect screenBounds=[[UIScreen mainScreen] bounds];
18 // CGRect windowBounds=CGRectMake(0, 0, 320, 150);
19 //windowBounds.origin.y=0;
20 self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
21 self.viewController=[[[SlideViewController alloc]init]autorelease];
22 self.window.backgroundColor = [UIColor blackColor];
23
24 [self.window addSubview:self.viewController.view];
25 [self.window makeKeyAndVisible];
26 return YES;
27 }









posted @ 2012-01-21 23:23  alex hu  阅读(887)  评论(0编辑  收藏  举报