Powered By Blogger

Thursday, April 7, 2011

Custom Alert View (UIAlertView)

  ************************** Starting Of Header File Part ***************************


 #import <UIKit/UIKit.h>
#define OVALSCALEX 1.0
#define OVALSCALEY 70.0/425.0

@interface CustomAleartView : UIAlertView <UIAlertViewDelegate> {

}
- (void) drawRoundedRect:(CGRect) rect
          withBackground:(UIColor *) backgroundColor
               andBorder:(UIColor *) borderColor
               andRadius:(CGFloat) radius
              andContext:(CGContextRef)cntxt;

@end

 ************************** Starting Of Implementation Part ***************************

#import "CustomAleartView.h"


@implementation CustomAleartView


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}
- (void)willPresentAlertView:(UIAlertView *)alertView{
   
    self.frame = CGRectMake(10, 20, 420, 300);
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext ();
    [self drawRoundedRect:rect withBackground:[UIColor blackColor] andBorder:[UIColor whiteColor] andRadius:10.0  andContext:context];
}

- (void) drawRoundedRect:(CGRect) rect
          withBackground:(UIColor *) backgroundColor
               andBorder:(UIColor *) borderColor
               andRadius:(CGFloat) radius
              andContext:(CGContextRef)cntxt
{
    CGContextRef context = cntxt;
   
    CGContextSaveGState (context);
   
    //Calculate rounded form
    CGMutablePathRef     roundedPath = CGPathCreateMutable ();
   
    CGPathMoveToPoint (roundedPath, NULL, CGRectGetMinX (rect) + radius, CGRectGetMinY (rect));
    CGPathAddArc (roundedPath, NULL, CGRectGetMaxX (rect)-radius, CGRectGetMinY (rect)+radius, radius, 3*M_PI/2, 0, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMaxX (rect)-radius, CGRectGetMaxY (rect)-radius, radius, 0, M_PI/2, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMinX (rect)+radius, CGRectGetMaxY (rect)-radius, radius, M_PI/2, M_PI, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMinX (rect)+radius, CGRectGetMinY (rect)+radius, radius, M_PI, 3*M_PI/2, 0);    
    CGPathCloseSubpath (roundedPath);
   
   
   
    //If background defined, fill roundedPath and add some light effect from the top
   
    if (backgroundColor)
    {
        //Fill background
        CGContextSetFillColorWithColor (context, [backgroundColor CGColor]);
        CGContextAddPath (context, roundedPath);
        CGContextFillPath (context);
       
        //Clip area
        CGContextAddPath (context, roundedPath);         
        CGContextClip (context);
       
       
        //     Draw elipse
        CGContextSetFillColorWithColor (context, [[[UIColor whiteColor] colorWithAlphaComponent:40.0/255.0] CGColor]);
       
       
       
        CGContextScaleCTM (context, OVALSCALEX, OVALSCALEY);
        CGContextAddArc (context,
                         (rect.origin.x+rect.size.width/2.0)/OVALSCALEX,
                         (rect.origin.y+0.0)/OVALSCALEY,
                         rect.size.width/2.0*425.0/270.0,
                         0.0, 2*M_PI, 1);
        CGContextFillPath (context);
       
        CGContextRestoreGState (context);     // Reset CTM
        CGContextSaveGState (context);         
    }
   
    if (borderColor)
    {
        CGContextSetStrokeColorWithColor (context, [borderColor CGColor]);
        CGContextSetLineWidth (context, 3.0);
        CGContextAddPath (context, roundedPath);
        CGContextStrokePath (context);
    }
   
    CFRelease (roundedPath);
   
    CGContextRestoreGState (context);
}


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

@end

UIKeyborad Notification

Hi friends,
This informs you how to use UIKeyboardNotification into your project. By using this , you can make your custom animation at the time of keyboard appears/disappears.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                               name:UIKeyboardWillShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardShow:(NSNotification *)aNotification
{
    // the keyboard is showing so resize the table's height
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.size.height -= keyboardRect.size.height;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)keyboardHide:(NSNotification *)aNotification
{
    // the keyboard is hiding reset the table's height
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.size.height += keyboardRect.size.height;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

If any one has query then place you it over here.

Thanks and Regards,
Nilesh Prajapati
 

UIViewAnimation Transition Code

    UIView *kView;

    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"Right Flip" context:context];
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:kView cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];