Thursday, 26 November 2015

UILabel touch event

UILabel *label = [[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
      [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];

Monday, 23 November 2015

UIView With Multiple Colors iOS


-(void)drawRect:(CGRect)rect {
    CGRect upperRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height * .5);
    CGRect lowerRect = CGRectMake(rect.origin.x, rect.origin.y + (rect.size.height * .5), rect.size.width, rect.size.height *(1-.5));

    [[UIColor redColor] set];
    UIRectFill(upperRect);
    [[UIColor greenColor] set];
    UIRectFill(lowerRect);
}

Thursday, 19 November 2015

UIView transition animation


-(void)SwipeRight:(UIView *)view{
    
    CATransition* transition = [CATransition animation];
    [transition setDuration:0.5];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    [transition setFillMode:kCAFillModeBoth];
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:transition forKey:kCATransition];
  
}

-(void)SwipeLeft:(UIView *)view{
    CATransition* transition = [CATransition animation];
    [transition setDuration:0.5];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setFillMode:kCAFillModeBoth];
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:transition forKey:kCATransition];

}

(OR)

- (void)slideView:(UIView*)view direction:(BOOL)isLeftToRight {
    CGRect frame = view.frame;
    frame.origin.x = (isLeftToRight) ? -
[[UIScreen mainScreen]bounds].size.width : 
[[UIScreen mainScreen]bounds].size.width;
    view.frame = frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    frame.origin.x = 0;
    view.frame = frame;
    [UIView commitAnimations];
}

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;
    }];
}