Friday, 6 November 2015

custom button

UIViewController.h

#import "CustomView.h"

@interface ViewController : UIViewController<CustomViewDelegate>
@property (strong, nonatomic)IBOutlet CustomView *btn1;

@end

UIViewController.m

@interface ViewController ()
{
    dispatch_time_t popTime1;
    NSTimer * myTimer ;
}
@end

/*
//UIViewController you create customview as addsubview you will use initWithFrame.

-(void)loadView
{
    [super loadView];
    CustomView *view = [[CustomView alloc]initWithFrame:self.view.bounds];
    view.delegate = self;
    [self.view addSubview:view];
    
}
 */


-(void)sendButtonPressed{    
    [btn1 indicator];
   popTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
    dispatch_after(popTime1, dispatch_get_main_queue(), ^(void){
        [btn1 resultSuccess];
    });
  myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(aTime) userInfo:nil repeats:YES];
}

-(void)aTime
{
    [myTimer invalidate];
    [btn1 saveBack];
}



UIView 
CustomView.h
@protocol CustomViewDelegate <NSObject>
-(void)sendButtonPressed;
@end

@interface CustomView : UIView
@property (assign) id<CustomViewDelegate> delegate;
//@property (strong, nonatomic) id<CustomViewDelegate> delegate;
@property (strong, nonatomic) UIButton* sendButton;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
-(void)indicator;
-(void)resultSuccess;
-(void)saveBack;
@end

CustomView.m

/*
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
       // self.backgroundColor = [UIColor whiteColor];
[self btnview];
      }
    return self;
}
*/

-(void)btnview{
    sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [sendButton addTarget:self.delegate action:@selector(sendButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [sendButton setTitle:@"save" forState:UIControlStateNormal];
    [sendButton setBackgroundColor:[UIColor blueColor]];
    [self addSubview:sendButton];
}

//UIViewController you create customview as xib you will use awakeFromNib

- (void) awakeFromNib
{
    [self btnview];
}

-(void)indicator
{    
   activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.alpha = 1.0;
    activityIndicator.center = CGPointMake(20,20);
    activityIndicator.hidesWhenStopped = NO;
    [sendButton addSubview:activityIndicator];
    [activityIndicator startAnimating];
    sendButton.userInteractionEnabled = NO;
}

-(void)resultSuccess{
    activityIndicator.hidesWhenStopped = YES;
    [activityIndicator stopAnimating];
    [sendButton setBackgroundColor:[UIColor grayColor]];
    [sendButton setTitle:@"success" forState:UIControlStateNormal];
    
    [UIView transitionWithView:sendButton duration:0.3 options:UIViewAnimationOptionTransitionFlipFromBottom|UIViewAnimationOptionCurveEaseInOut animations:^{
        [sendButton setAlpha:0.0];
        [sendButton setAlpha:1.0];
    } completion:^(BOOL finished){
       
    }];
   
}

-(void)saveBack{
    [sendButton setTitle:@"save" forState:UIControlStateNormal];
    [sendButton setBackgroundColor:[UIColor blueColor]];
    [UIView transitionWithView:sendButton duration:0.3 options:UIViewAnimationOptionTransitionFlipFromTop|UIViewAnimationOptionCurveEaseInOut animations:^{
        [sendButton setAlpha:0.0];
        [sendButton setAlpha:1.0];
    } completion:^(BOOL finished){
      sendButton.userInteractionEnabled = YES;
    }];
}

No comments:

Post a Comment