2016-10-24 23 views
0

我想創建我自己的自定義顯示案例視圖,其中我有左鍵,右鍵和標籤欄視圖。創建顯示案例視圖在Tabbar xcode

我想突出顯示與它周圍的圓形按鈕。 我可以通過子類UIView 在我的按鈕周圍創建一個圓圈。但是,我這樣做的用戶從左到右滑動,它會將圓圈更改爲右側按鈕,左側按鈕隱藏。 這裏是我的UIView

//--------.h類的子類

#import <UIKit/UIKit.h> 

@interface IntroView : UIView 
- (void)drawRect:(CGRect)rect withBtnRect:(CGRect)btnrect ; 

@end 

// ------。米類

#import "IntroView.h" 

@implementation IntroView 

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


    self.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.3]; 

    return self; 
} 



- (void)drawRect:(CGRect)rect { 


    CGRect maskRect= CGRectMake(5, 5, 100, 100);//set and pass this maskRect 


    CGRect rBounds = self.bounds; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Fill background with 40% gray 
    CGContextSetFillColorWithColor(context, [[[UIColor grayColor] colorWithAlphaComponent:0.4] CGColor]); 
    CGContextFillRect(context, rBounds); 

    // Draw the window 'frame' 
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]); 
    CGContextSetLineWidth(context,2); 
    CGContextStrokeEllipseInRect(context, maskRect); 

    // make the window transparent 
    CGContextSetBlendMode(context, kCGBlendModeClear); 
    CGContextFillEllipseInRect (context, maskRect); 

    } 




/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

而我的TabBar第一視圖內我請使用

#import "IntroView.h" 

然後

- (void)viewDidLoad 
{ 
    IntroView *vw_intro=[[IntroView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    vw_intro.tag=1000;  
    [self.view addSubview:vw_intro]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

這是我的輸出

enter image description here

和預期輸出幻燈片上從右至左,所以還是反向(就像我們做showcase android library

enter image description here

回答

1

在IntroView.h

#import <UIKit/UIKit.h> 

@interface IntroView : UIView 
- (id)initWithFrame:(CGRect)frame introElements:(NSArray *)iElements; 
- (void)showIntro; 
@end 

在IntroView.m

#import "IntroView.h" 

@implementation IntroView 
{ 
    CAShapeLayer *mask; 
    NSArray *introElements; 
    NSUInteger currentIndex; 
} 

- (id)initWithFrame:(CGRect)frame introElements:(NSArray *)iElements 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     // Initialization code 

     introElements = iElements; 

     mask = [CAShapeLayer layer]; 
     [mask setFillColor:[[UIColor grayColor] colorWithAlphaComponent:0.8f].CGColor]; 
     [mask setFillRule:kCAFillRuleEvenOdd]; 
     [self.layer addSublayer:mask]; 

     UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipe)]; 
     swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
     [self addGestureRecognizer:swipeGestureRecognizer]; 
    } 

    return self; 
} 

- (void)userDidSwipe 
{ 
    [self showIntroAtIndex:currentIndex+1]; 
} 

- (void)showIntro 
{ 
    //Initial index by default 
    [self showIntroAtIndex:0]; 
} 

- (void)showIntroAtIndex:(NSUInteger)index 
{ 
    currentIndex = index; 

    CGRect rect = [[introElements objectAtIndex:index] CGRectValue]; 

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds]; 
    UIBezierPath *elementPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0f]; 
    [maskPath appendPath:elementPath]; 

    // Animate 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"]; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    animation.duration = 0.3f; 
    animation.fromValue = (__bridge id)(mask.path); 
    animation.toValue = (__bridge id)(maskPath.CGPath); 
    [mask addAnimation:animation forKey:@"path"]; 
    mask.path = maskPath.CGPath; 
} 

在ViewController

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSArray *viewElements = [NSArray arrayWithObjects:[NSValue valueWithCGRect:button1.frame],[NSValue valueWithCGRect:button2.frame], nil]; 

    IntroView *introView = [[IntroView alloc]initWithFrame:self.view.bounds introElements:viewElements]; 
    [self.view addSubview:introView]; 
    [introView showIntro]; 
} 

注意:我突出了UIControl框架,如果您需要圓形高亮顯示,請更新CGRect中的更改。

+0

感謝一個漂亮又快速的答案,我會將它轉換爲圓形並很快發佈 – iphonemaclover