iOS 7定製UIPageControl遇到的問題

轉自snorlax's blogios

先說下ios7以前 那些點點的實現很是簡單 就是UIPageControl.subviews 就是一個個點的UIImageView 因此只需簡單的替換掉就行了代碼以下:@property(nonatomic,strong)UIImage *_activeImage;app

@property(nonatomic,strong)UIImage *_inactiveImage;
//在init的時候把你本身定義的兩張圖片搞進去
- (void)updateDots
{
    for (int i = 0; i< [self.subviews count]; i++) {
        UIImageView* dot =[self.subviews objectAtIndex:i];
 
        if (i == self.currentPage) {
            if ([dot respondsToSelector:@selector(setImage:)]) {
                dot.image=_activeImage;
            }
 
        } else {
            if ([dot respondsToSelector:@selector(setImage:)]) {
                dot.image=_inactiveImage;
            }
        }
    }
}
//而後本身重載一下setNumber就行了
 
- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    [super setNumberOfPages:numberOfPages];
    [self updateDots];
}

ios7改掉了連底層實現……整個pagecontrol 的subviews是UIView亂七八糟……並且都是view根本就沒了image屬性、
因此照樣使用ios7以前的定製圖片pagecontrol點的方法會出錯。

解決方法:就要重寫UIView的繪畫函數了代碼大體以下

#import "ZDPageControll.h"
#import <QuartzCore/QuartzCore.h>
 
@interface ZDPageControll()
{
    UIImage *_activeImage;
    UIImage *_inactiveImage;
    NSArray *_usedToRetainOriginalSubview;
}
 
@end
 
@implementation ZDPageControll
@synthesize kSpacing=_kSpacing;
- (id)initWithFrame:(CGRect)frame currentImageName:(NSString *)current commonImageName:(NSString *)common
{
    self= [super initWithFrame:frame];
    if ([self respondsToSelector:@selector(setCurrentPageIndicatorTintColor:)] && [self respondsToSelector:@selector(setPageIndicatorTintColor:)]) {
        [self setCurrentPageIndicatorTintColor:[UIColor clearColor]];
        [self setPageIndicatorTintColor:[UIColor clearColor]];
    }
 
    [self setBackgroundColor:[UIColor clearColor]];
    _activeImage= [UIImage imageNamed:current];
    _inactiveImage= [UIImage imageNamed:common];
    _kSpacing=10.0f;
    //hold住原來pagecontroll的subview
    _usedToRetainOriginalSubview=[NSArray arrayWithArray:self.subviews];
    for (UIView *su in self.subviews) {
        [su removeFromSuperview];
    }
    self.contentMode=UIViewContentModeRedraw;
    return self;
}
-(void)dealloc
{
    //釋放原來hold住的那些subview
    _usedToRetainOriginalSubview=nil;
    _activeImage=nil;
    _inactiveImage=nil;
}
- (void)updateDots
{
 
    for (int i = 0; i< [self.subviews count]; i++) {
        UIImageView* dot =[self.subviews objectAtIndex:i];
 
        if (i == self.currentPage) {
            if ([dot respondsToSelector:@selector(setImage:)]) {
                dot.image=_activeImage;
            }
 
        } else {
            if ([dot respondsToSelector:@selector(setImage:)]) {
                dot.image=_inactiveImage;
            }
        }
    }
}
 
- (void)setCurrentPage:(NSInteger)currentPage
{
    [super setCurrentPage:currentPage];
    if ([[[UIDevice currentDevice]systemVersion]floatValue] <=6.0) {
        [self updateDots];
    }
//    [self updateDots];
    [self setNeedsDisplay];
}
- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    [super setNumberOfPages:numberOfPages];
    if ([[[UIDevice currentDevice]systemVersion]floatValue] <=6.0) {
        [self updateDots];
    }
//    [self updateDots];
    [self setNeedsDisplay];
 
}
-(void)drawRect:(CGRect)iRect
{
    if ([kIphoneVersionfloatValue]>=7.0){//加個判斷
    int i;
    CGRect rect;
 
    UIImage *image;
    iRect = self.bounds;
 
    if ( self.opaque ) {
        [self.backgroundColor set];
        UIRectFill( iRect );
    }
 
    if ( self.hidesForSinglePage && self.numberOfPages == 1 ) return;
 
    rect.size.height = _activeImage.size.height;
    rect.size.width = self.numberOfPages * _activeImage.size.width + ( self.numberOfPages - 1 ) * _kSpacing;
    rect.origin.x = floorf( ( iRect.size.width - rect.size.width ) / 2.0 );
    rect.origin.y = floorf( ( iRect.size.height - rect.size.height ) / 2.0 );
    rect.size.width = _activeImage.size.width;
 
    for ( i = 0; i < self.numberOfPages; ++i ) {
        image = i == self.currentPage ? _activeImage : _inactiveImage;
 
        [image drawInRect: rect];
 
        rect.origin.x += _activeImage.size.width + _kSpacing;
    }

    }else {ide

    }函數

}
@end
相關文章
相關標籤/搜索