2012-10-25 50 views
8

我想讓練習應用程序可以在頁面控制中滾動圖像。我能夠滾動圖像並能夠包含頁面控件。但是我面臨的問題是我無法將兩者聯繫起來。這意味着當我滾動圖像時,頁面控件不受影響,當我更改頁面控件時,圖像的滾動不受影響。用iPhone中的頁面控制滑動圖像

我提到了這個:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/用於頁面控制的滾動。

Viewcontroller.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UIScrollViewDelegate>{ 
    UIScrollView *scrollView; 
    UIPageControl *pageControl; 

    BOOL pageControlBeingUsed; 
} 

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView; 
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl; 

- (IBAction)changePage; 

@end 

Viewcontroller.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize scrollView,pageControl; 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    if (!pageControlBeingUsed) { 
     // Switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = self.scrollView.frame.size.width; 
     int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
     self.pageControl.currentPage = page; 
    } 
} 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
    pageControlBeingUsed = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    pageControlBeingUsed = NO; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg" ], nil]; 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height); 

    for (int i = 0; i < images.count; i++) { 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     UIImageView* imgView = [[UIImageView alloc] init]; 
     imgView.image = [images objectAtIndex:i]; 
     imgView.frame = frame; 
     [scrollView addSubview:imgView]; 
    } 

    self.pageControl.currentPage = 0; 
    self.pageControl.numberOfPages = images.count; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.scrollView = nil; 
    self.pageControl = nil; 
} 

- (IBAction)changePage{ 
     // update the scroll view to the appropriate page 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
     pageControlBeingUsed = YES; 
} 

@end 

需要一些指導這個...謝謝..

回答

3

檢查你是否已設定滾動視圖的委託到ViewController

而你剛剛設置pageControlBeingUsed爲YES或者NO,你還沒有使用它在視圖控制器

+0

在哪裏使用它?可以給我一個在哪裏使用它的想法? – lakesh

+0

現在改變了代碼...包括pageControlBeingUsed在scrollViewDidScroll函數..仍然沒有區別... – lakesh

+0

道歉...設置我的代表錯誤..感謝您的幫助... – lakesh