2010-04-10 19 views
0

我有使用UIPageControl一些奇怪的行爲:UIPageControl錯誤:第一顯示一顆子彈,然後顯示一切

初看上去似乎只顯示一個子彈,然後當我把所有的子彈正確顯示滾動視圖。在我將它作爲子視圖添加之前,是否有某些東西丟失?

這裏是我的代碼imageScrollView.h:

@interface ImageScrollView : UIView <UIScrollViewDelegate> { 
    NSMutableDictionary *photos; 
    BOOL *pageControlIsChangingPage; 
    UIPageControl *pageControl; 
} 
@property (nonatomic, copy) NSMutableDictionary *photos; 
@property (nonatomic, copy) UIPageControl *pageControl; 
@end 

這裏是imageScrollView.m代碼:

#import "ImageScrollView.h" 


@implementation ImageScrollView 
@synthesize photos, pageControl; 

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
    // Initialization code 
    } 
    return self; 
} 

- (void) drawRect:(CGRect)rect 
{ 
    [self removeAllSubviews]; 

    UIScrollView *scroller = [[UIScrollView alloc]  initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)]; 
    [scroller setDelegate:self]; 

    [scroller setBackgroundColor:[UIColor grayColor]]; 

    [scroller setShowsHorizontalScrollIndicator:NO]; 
    [scroller setPagingEnabled:YES]; 

    NSUInteger nimages = 0; 
    CGFloat cx= 0; 

    for (NSDictionary *myDictionaryObject in photos) 
    { 
    if (![myDictionaryObject isKindOfClass:[NSNull class]]) { 
    NSString *photo =[NSString stringWithFormat:@"http://www.techbase.com.mx/blog/%@",[myDictionaryObject objectForKey:@"filepath"]]; 
    NSDictionary *data = [myDictionaryObject objectForKey:@"data"]; 

    UIView *imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 30)]; 

    TTImageView *imageView = [[TTImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 30)]; 
    imageView.urlPath=photo; 
    [imageContainer addSubview:imageView]; 

    UILabel *caption = [[UILabel alloc] initWithFrame:CGRectMake(0,imageView.frame.size.height,imageView.frame.size.width,10)]; 

    [caption setText:[NSString stringWithFormat:@"%@",[data objectForKey:@"description"]]]; 
    [caption setBackgroundColor:[UIColor grayColor]]; 
    [caption setTextColor:[UIColor whiteColor]]; 
    [caption setLineBreakMode:UILineBreakModeWordWrap]; 
    [caption setNumberOfLines:0]; 
    [caption sizeToFit]; 
    [caption setFont:[UIFont fontWithName:@"Georgia" size:10.0]]; 
    [imageContainer addSubview:caption]; 

    CGRect rect = imageContainer.frame; 
    rect.size.height = imageContainer.size.height; 
    rect.size.width = imageContainer.size.width; 
    rect.origin.x = ((scroller.frame.size.width - scroller.size.width)/2) + cx; 
    rect.origin.y = ((scroller.frame.size.height - scroller.size.height)/2); 
    imageContainer.frame=rect; 
    [scroller addSubview:imageContainer]; 
    [imageView release]; 
    [imageContainer release]; 
    [caption release]; 

    nimages++; 
    cx +=scroller.frame.size.width; 

    } 

    } 

    [scroller setContentSize:CGSizeMake(nimages * self.frame.size.width, self.frame.size.height)]; 
    [self addSubview:scroller]; 
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height -20, (self.frame.size.width/nimages)/2, 20)]; 
    pageControl.numberOfPages=nimages; 
    [self addSubview:pageControl]; 

    [scroller release]; 

    } 

    -(void)dealloc { 
    [pageControl release]; 
    [super dealloc]; 
    } 


    -(void)scrollViewDidScroll:(UIScrollView *)_scrollView{ 
    if(pageControlIsChangingPage){ 
    return; 
    } 
    CGFloat pageWidth = _scrollView.frame.size.width; 
    int page = floor((_scrollView.contentOffset.x - pageWidth /2)/pageWidth) + 1; 
    pageControl.currentPage = page; 
    } 

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{ 
pageControlIsChangingPage = NO; 
} 

@end 

回答

1

既然你在drawRect方法繪製UIPageControl,你需要初始化後,請調用此控件的setNeedsLayout方法。否則,它將不會正確渲染自己,直到調用此重繪的事件被調用。

pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height -20, (self.frame.size.width/nimages)/2, 20)]; 
pageControl.numberOfPages=nimages; 
[pageControl setNeedsLayout]; 
[self addSubview:pageControl]; 
+0

它沒有工作的人! :( 同樣的問題 – user313551 2010-04-12 22:06:33

相關問題