Monday, 29 April 2013

Zoom IN & Zoom OUT using a View in XCode

Description:

This code can be used to Zoom In & Zoom Out options for View.


.h File:


@interface Zooming : UIViewController
{
    IBOutlet UIScrollView *scrollView;
 
    IBOutlet UIView *viewer;
}
@end



.m File:


- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIButton *add = [[UIButton alloc]init];
    add.frame = CGRectMake(10, 10, 100, 100);
    [add setTitle:@"add" forState:UIControlStateNormal];
    add.backgroundColor = [UIColor brownColor];
 
    UIButton *delete = [[UIButton alloc]init];
    delete.frame = CGRectMake(200, 200, 100, 100);
    [delete setTitle:@"delete" forState:UIControlStateNormal];
    delete.titleLabel.textColor = [UIColor blackColor];
    delete.backgroundColor = [UIColor greenColor];
 
    viewer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1000, 1000)];
    [viewer addSubview:add];
    [viewer addSubview:delete];
 
    CGRect yFrame = CGRectMake(0.0, 0.0, 320, 410);
    scrollView = [[UIScrollView alloc] initWithFrame:yFrame];
    [scrollView setScrollEnabled:YES];
    [scrollView setBackgroundColor:[UIColor clearColor]];
    [scrollView setContentSize:CGSizeMake(1000, 1000)];
    scrollView.minimumZoomScale = 0.3;
    scrollView.maximumZoomScale = 2.0;
    scrollView.zoomScale = 0.3;
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    scrollView.delegate = self;
    [scrollView addSubview:viewer];


    // For set a Zoom out the view
    //scrollView.zoomScale = 0.4;

 
    [self.view addSubview:scrollView];
    [self.view bringSubviewToFront:scrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return viewer;
}


No comments: