Friday, 14 February 2014

remove subview with rotating animation

Hello All,

Last week i have to do some animation in my application like zoom, rotate etc. I here share one of my trick to add object in view with some animation.

My goal is to remove view from its super view with rotating animation.

We will achieve that goal by using timer.We are using these two methods.

- (void) removeWithSinkAnimation:(int)steps;


- (void) removeWithSinkAnimationRotateTimer:(NSTimer*) timer;


assuming that we have added our view to the screen and we want to remove it with animation.

- (void) removeWithSinkAnimation:(int)steps{
NSTimer *timer;
if (steps > 0 && steps < 100) // just to avoid too much steps
self.tag = steps;
else
self.tag = 50;
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(removeWithSinkAnimationRotateTimer:) userInfo:nil repeats:YES];

}

- (void) removeWithSinkAnimationRotateTimer:(NSTimer*) timer{
CGAffineTransform trans = CGAffineTransformRotate(CGAffineTransformScale(self.transform, 0.9, 0.9),0.314);
self.transform = trans;
self.alpha = self.alpha * 0.98;
self.tag = self.tag - 1;
if (self.tag <= 0)
{
[timer invalidate];
[view removeFromSuperview];

}
}

Thanks. 

Friday, 25 October 2013

CustomMenu

In my one of the application there is a need to display custom menu  to select search categories or etc menus.You can achieve this using making custom control extending UIView.

To achieve this please import .h and .m files from here.

you can get images related to this customMenu from here

Download


CustomMenuView.h

#import <Foundation/Foundation.h>


typedef enum {
 CustomMenuArrowAlignmentCenter,
 CustomMenuArrowAlignmentLeft,
 CustomMenuArrowAlignmentRight
} CustomMenuArrowAlignment;


@protocol CustomMenuViewDelegate
- (void)CustomMenuButtonTappedWithTitle:(NSString*)title;
@end;

@interface CustomMenuView : UIView
{
    //for ios 5.0 issue
    __unsafe_unretained id<CustomMenuViewDelegate>delegate;
    UIView *viewPopOver;
    UIImageView *imgArrow;
    
}
@property (nonatomic, assign)id <CustomMenuViewDelegate> delegate;
@property (nonatomic, strong)UIImageView *imgArrow;

-(IBAction)menuButtonTapped:(id)sender;
-(void)setArrowPosition:(CustomMenuArrowAlignment)position;
-(void)addMenuButtonsWithTitles:(NSArray*)titles;
-(void)setMenuPosition:(CGPoint)point;

-(void)setOptionsWithTitles:(NSArray*)titles andPosition:(CGPoint)position andArrowPosition:(CustomMenuArrowAlignment)alignmetposition;
@end

CustomMenuView.m

#import "CustomMenuView.h"

@implementation CustomMenuView
@synthesize delegate;
@synthesize imgArrow;

#define ARROW_WIDTH 19.0
#define ARROW_HEIGHT 9.0

#define POPOVER_VIEW_WIDTH 110

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])){
        
        [self setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_43.png"]];
        [self.layer setOpaque:NO];
        self.opaque = NO;
        
        imgArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bo_errow.png"]];
        [self addSubview:imgArrow];
        
        viewPopOver = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
        [self addSubview:viewPopOver];
    }
    return self;
}

-(void)setOptionsWithTitles:(NSArray*)titles andPosition:(CGPoint)position andArrowPosition:(CustomMenuArrowAlignment)alignmetposition{
    
    [self addMenuButtonsWithTitles:titles];
    [self setMenuPosition:position];
    [self setArrowPosition:alignmetposition];
}

-(void)addMenuButtonsWithTitles:(NSArray*)titles{
    
    int x = 2.5;
    int y = 4;
    
    int buttonHeight = 21;
    int buttonWidth = 106;
    
    NSString *strbuttonTitle;
    
    for (UIView *view in [viewPopOver subviews]) {
        if ([view isKindOfClass:[UIButton class]]){
            [view removeFromSuperview];
        }
    }
    
    for(int i=0;i<[titles count];i++){
        
        UIButton *sub_button = [UIButton buttonWithType:UIButtonTypeCustom];
        strbuttonTitle = [NSString stringWithFormat:@"%@",[titles objectAtIndex:i]];
        
        [sub_button setTitle:strbuttonTitle forState:UIControlStateNormal];
        [sub_button setFrame:CGRectMake(x, y, buttonWidth, buttonHeight)];
        
        [sub_button setBackgroundImage:[UIImage imageNamed:@"btn_43_normal.png"] forState:UIControlStateNormal];
        [sub_button setBackgroundImage:[UIImage imageNamed:@"btn_43_selected.png"] forState:UIControlStateHighlighted];
        [sub_button.titleLabel setFont:[UIFont systemFontOfSize:11]];
        [sub_button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [sub_button.titleLabel setTextAlignment:UITextAlignmentCenter];
        sub_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        sub_button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        CGFloat width = sub_button.frame.size.width ;
        CGSize theSize;
        theSize = [strbuttonTitle sizeWithFont:[UIFont systemFontOfSize:11] constrainedToSize:CGSizeMake(width, 40) lineBreakMode:UILineBreakModeWordWrap];
        
        CGFloat myLabelHeight = theSize.height;
        
        if(myLabelHeight>14){
            [sub_button setBackgroundImage:[UIImage imageNamed:@"btn_43_normal_double.png"] forState:UIControlStateNormal];
            [sub_button setBackgroundImage:[UIImage imageNamed:@"btn_43_selected_double.png"] forState:UIControlStateHighlighted];
            buttonHeight =41;
            [sub_button setFrame:CGRectMake(x, y, buttonWidth, buttonHeight)];
        }
        [sub_button addTarget:self action:@selector(menuButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        
        y = y + buttonHeight + 4;
        
        [viewPopOver addSubview:sub_button];
    }
    
    int totalHeight = 0.0;
    totalHeight = y;
    
    [viewPopOver setFrame:CGRectMake(viewPopOver.frame.origin.x, viewPopOver.frame.origin.y, POPOVER_VIEW_WIDTH,totalHeight)];
    
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = viewPopOver.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:67.0/255 green:105.0/255 blue:142.0/255 alpha:1] CGColor], (id)[[UIColor colorWithRed:34.0/255 green:69.0/255 blue:108.0/255 alpha:1] CGColor], nil];
    
    gradient.cornerRadius = 4.0;
    
    [viewPopOver.layer insertSublayer:gradient atIndex:0];
    
    viewPopOver.layer.cornerRadius = 4.0;
    viewPopOver.clipsToBounds = YES;
}

-(void)setMenuPosition:(CGPoint)point{
    CGRect frame = [viewPopOver frame];
frame.origin.x = point.x;
frame.origin.y = point.y;
[viewPopOver setFrame:frame];
}

-(void)setArrowPosition:(CustomMenuArrowAlignment)position{
    
    if(position == CustomMenuArrowAlignmentCenter){
        [imgArrow setFrame:CGRectMake(viewPopOver.frame.origin.x+(viewPopOver.frame.size.width/2-ARROW_WIDTH/2), viewPopOver.frame.origin.y-ARROW_HEIGHT, ARROW_WIDTH, ARROW_HEIGHT)];
    }
    else if(position == CustomMenuArrowAlignmentLeft){
        [imgArrow setFrame:CGRectMake(viewPopOver.frame.origin.x+20.0, viewPopOver.frame.origin.y-ARROW_HEIGHT, ARROW_WIDTH, ARROW_HEIGHT)];
        
    }else if(position == CustomMenuArrowAlignmentRight){
        [imgArrow setFrame:CGRectMake(viewPopOver.frame.origin.x+70.0, viewPopOver.frame.origin.y-ARROW_HEIGHT, ARROW_WIDTH, ARROW_HEIGHT)];
    }
}


-(IBAction)menuButtonTapped:(id)sender{
    UIButton *btn = sender;
    [delegate CustomMenuButtonTappedWithTitle:[btn titleForState:UIControlStateNormal]];
    [self removeFromSuperview];
}

@end


To use this menu in  specific viewController implement CustomMenuViewDelegate protocol and on buttonclick implement this code like this to display custommenu :

-(IBAction)showStatusFilterOptions:(id)sender{
    
    CustomMenuView *oldCustomMenu = (CustomMenuView*)[self.view viewWithTag:TAG_CUSTOMMENU];
    if (oldCustomMenu != nil) {
        [oldCustomMenu removeFromSuperview];
    }
    else{
        CustomMenuView *cMenu = [[CustomMenuView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)];
        cMenu.tag = TAG_CUSTOMMENU;
        [cMenu setOptionsWithTitles:[NSArray arrayWithObjects:@"First",@"Second",@"Third",nil] andPosition:CGPointMake(200, 8) andArrowPosition:CustomMenuArrowAlignmentRight];
        cMenu.delegate = self;
        
        [self.view addSubview:cMenu];
    }
}

when user taps on perticuler menu if you want to do perticuler action:
#pragma mark - Custom menu view delegate method

- (void)CustomMenuButtonTappedWithTitle:(NSString*)title{
    NSString *status = @"";
    status = title;
  if ([title isEqualToString:@"First"]) {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@ is clicked",status] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
}


The full code is here 

SettingsMenuViewController.h

#import <UIKit/UIKit.h>
#import "CustomMenuView.h"
@interface SettingsMenuViewController : UIViewController<CustomMenuViewDelegate>
{
}
@end

#import "SettingsMenuViewController.h"
#define TAG_CUSTOMMENU 9999
@interface SettingsMenuViewController ()

@end


SettingsMenuViewController.m 

@implementation SettingsMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *bkgImage = [UIImage imageNamed:@"Default.png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bkgImage];
    self.view.contentMode = UIViewContentModeScaleAspectFill;
    [self.view setOpaque:YES];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)showStatusFilterOptions:(id)sender{
    
    CustomMenuView *oldCustomMenu = (CustomMenuView*)[self.view viewWithTag:TAG_CUSTOMMENU];
    if (oldCustomMenu != nil) {
        [oldCustomMenu removeFromSuperview];
    }
    else{
        CustomMenuView *cMenu = [[CustomMenuView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)];
        cMenu.tag = TAG_CUSTOMMENU;
        [cMenu setOptionsWithTitles:[NSArray arrayWithObjects:@"First",@"Second",@"Third",nil] andPosition:CGPointMake(200, 8) andArrowPosition:CustomMenuArrowAlignmentRight];
        cMenu.delegate = self;
        
        [self.view addSubview:cMenu];
    }
}
#pragma mark - Custom menu view delegate method

- (void)CustomMenuButtonTappedWithTitle:(NSString*)title{
    NSString *status = @"";
    status = title;
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@ is clicked",status] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

@end

Friday, 18 October 2013

Zoom the View in ios application

There are inbuilt so many animation types availbale to apply in views, but here we are applying zoom effect in ImageView:

[UIView animateWithDuration:7.0 delay: 0 options: UIViewAnimationCurveEaseOut animations:^{ CGAffineTransform move = CGAffineTransformMakeTranslation(0, 0);
CGAffineTransform zoom = CGAffineTransformMakeScale(1.2, 1.2);
 CGAffineTransform transform = CGAffineTransformConcat(zoom, move);
 image_preview.transform = transform; // move
 }
 completion:^(BOOL finished){ image_preview.transform = CGAffineTransformIdentity; }]; }

Wednesday, 16 October 2013

Custom Gradient View

Custom Gradient Effect In View [ Iphone]

To give gradient effect to perticuler view we are using here CAGradientLayer
Compatibility:From ios 3.0 and later
1> First to use CAGradientLayer you have to import QuartzCore Framework.
2> To add gradient effect to peticuler view you can use this code:
  CAGradientLayer *gradient = [CAGradientLayer layer];
  gradient.frame = self.viewPopOver.bounds;
   gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:67.0/255 green:105.0/255    blue:142.0/255 alpha:1] CGColor], (id)[[UIColor colorWithRed:34.0/255 green:69.0/255     blue:108.0/255 alpha:1] CGColor], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];
 The result will be as shown below:

Image

Tuesday, 15 October 2013

Slide Animation in iPad


Like facebook application if we want to apply slide animation in our application we don't need to add split view controller ,we can do that by simply add animation for perticuler view

first define kMenuListingOriginForPortrait and kMenuListingOriginForLandscape in .pch file

#define kMenuListingOriginForPortrait 768
#define kMenuListingOriginForLandscape 1024

first set listing screen x origin kMenuListingOriginForPortrait in portrait and kMenuListingOriginForLandscape  in landscape mode then you can add on button and apply this method in that and via that method you can toggle[slide] your view 
-(IBAction)toggleListing
{
    
    [UIView beginAnimations:@"Menu Slide" context:nil];
    [UIView setAnimationDuration:0.5];
    CGRect newFrame;
    if(self.vw_Listing.frame.origin.x == kMenuListingOriginForPortrait || self.vw_Listing.frame.origin.x == kMenuListingOriginForLandscape){
        [self.vw_Listing setHidden:FALSE];
        newFrame = CGRectOffset(self.vw_Listing.frame, -(self.vw_Listing.frame.size.width), 0.0);
        self.vw_main.frame = CGRectMake(0, 0, self.vw_main.frame.size.width, self.vw_main.frame.size.height);
        vw_Listing.frame = newFrame;
        listingVisible = TRUE;
        masterVisible = FALSE;
        
    }
    else{
        newFrame = CGRectOffset(self.vw_main.frame, -(self.vw_Listing.frame.size.width), 0.0);
        self.vw_main.frame = newFrame;
        listingVisible = FALSE;
    }
    [UIView commitAnimations];
 }