Tuesday, June 4, 2013

UIScrollView bouncesZoom example in Objective C (iOS).


UIScrollView bouncesZoom

A Boolean value that determines whether the scroll view animates the content scaling when the scaling exceeds the maximum or minimum limits.

@property(nonatomic) BOOL bouncesZoom

Discussion of [UIScrollView bouncesZoom]
If the value of this property is YES and zooming exceeds either the maximum or minimum limits for scaling, the scroll view temporarily animates the content scaling just past these limits before returning to them. If this property is NO, zooming stops immediately at one a scaling limits. The default is YES .

UIScrollView bouncesZoom example.
- (void)loadView {
        [super loadView];
        leavesView.frame = self.view.bounds;
        leavesView.autoresizingMask = UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight;

        //begin
        scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:scrollview];
        [scrollview addSubview:leavesView];
        [scrollview setContentSize:leavesView.frame.size];
        [scrollview setDelegate:self];
        [scrollview setMinimumZoomScale:1.0];
        [scrollview setMaximumZoomScale:3.0];
        scrollview.bounces = NO;
        scrollview.bouncesZoom = YES;
        scrollview.zoomScale = lastScale;
        //end

Example of [UIScrollView bouncesZoom].
- (void)viewDidLoad {
AppDelegate *appDelegate = (pAppDelegate *)[[UIApplication sharedApplication] delegate];
[super viewDidLoad];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *ImagePath = [Path stringByAppendingPathComponent:(@"data: %@", appDelegate.MainImageName)];
UIImage *tempImg = [[UIImage alloc] initWithContentsOfFile:ImagePath];
[imgView setImage:tempImg];
myScrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
[myScrollView addSubview:myImageView];
//Set ScrollView Appearance
[myScrollView setBackgroundColor:[UIColor blackColor]];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//Set Scrolling Prefs
myScrollView.bounces = YES;
myScrollView.delegate = self;
myScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
[myScrollView setCanCancelContentTouches:NO];
[myScrollView setScrollEnabled:YES];
//Set Zooming Prefs
myScrollView.maximumZoomScale = 3.0;
myScrollView.minimumZoomScale = CGImageGetWidth(tempImg.CGImage)/320;
myScrollView.zoomScale = 1.01; //Added the .01 to enable scrolling immediately upon view load.
myScrollView.bouncesZoom = YES;
[myImageView setFrame:self.view.frame];//rect];// .frame.size.height = imageHeight;
myImageView.contentMode = UIViewContentModeCenter;
self.view = myScrollView;
[tempImg release];
}

UIScrollView bouncesZoom example.
-(id)initWithFrame:(CGRect)frame image:(UIImage*)_image {
if((self = [super initWithFrame:frame])) {
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
self.maximumZoomScale = 5.0f;
self.minimumZoomScale = 0.25f;
self.backgroundColor = [UIColor colorWithRed:0.4f green:0.2f blue:0.2f alpha:1.0f];
self.image = _image;

CGRect imageRect = CGRectMake(0.0f,0.0f,CGImageGetWidth(image.CGImage),CGImageGetHeight(image.CGImage));
imageScale = self.frame.size.width/imageRect.size.width;
minimumScale = imageScale * 0.75f;

NSLog(@"imageScale: %f",imageScale);
imageRect.size = CGSizeMake(imageRect.size.width*imageScale, imageRect.size.height*imageScale);

UIGraphicsBeginImageContext(imageRect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextDrawImage(context, imageRect, image.CGImage);
CGContextRestoreGState(context);

UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundImageView.frame = imageRect;
backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:backgroundImageView];
[self sendSubviewToBack:backgroundImageView];
frontTiledView = [[TiledImageView alloc] initWithFrame:imageRect image:image scale:imageScale];
[self addSubview:frontTiledView];
[frontTiledView release];
}
return self;
}

End of UIScrollView bouncesZoom example article.