Showing posts with label UIScrollView. Show all posts
Showing posts with label UIScrollView. Show all posts

Wednesday, June 5, 2013

UIScrollView UIScrollViewDecelerationRateNormal example in Objective C (iOS).


UIScrollView UIScrollViewDecelerationRateNormal

Deceleration Constants
The rate of deceleration for a scrolling view.

const float UIScrollViewDecelerationRateNormal;
const float UIScrollViewDecelerationRateFast;

Constants
UIScrollViewDecelerationRateNormal
The default deceleration rate for a scroll view.
UIScrollViewDecelerationRateFast
A fast deceleration rate for a scroll view.

UIScrollView UIScrollViewDecelerationRateNormal example.
NSLog(@"1. decelerationRate %f", scrollview.decelerationRate);

scrollview.decelerationRate = UIScrollViewDecelerationRateNormal;
NSLog(@"2. decelerationRate %f", scrollview.decelerationRate);

scrollview.decelerationRate = UIScrollViewDecelerationRateFast;
NSLog(@"3. decelerationRate %f", scrollview.decelerationRate);

scrollview.decelerationRate = 0.7;
NSLog(@"4. decelerationRate %f", scrollview.decelerationRate);

scrollview.decelerationRate = 0.995;
NSLog(@"5. decelerationRate %f", scrollview.decelerationRate);

Example of [UIScrollView UIScrollViewDecelerationRateNormal].
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        @try {
            CGFloat decelerationRate = UIScrollViewDecelerationRateFast +(UIScrollViewDecelerationRateNormal - UIScrollViewDecelerationRateFast) * .52;
            [self setValue:[NSValue valueWithCGSize:CGSizeMake(decelerationRate,decelerationRate)] forKey:@"_decelerationFactor"];

        }
        @catch (NSException *exception) {
            // if they modify the way it works under us.
        }

    }
    return self;
}

UIScrollView UIScrollViewDecelerationRateNormal example.
You can just turn up the deceleration rate very high. With an infinite rate, it would stop immediately. Try setting the rate to these constants:

scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
and

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

End of UIScrollView UIScrollViewDecelerationRateNormal example article.

UIScrollView UIScrollViewIndicatorStyleDefault example in Objective C (iOS).


UIScrollView UIScrollViewIndicatorStyleDefault

Scroll Indicator Style
The style of the scroll indicators. You use these constants to set the value of the indicatorStyle style.

typedef enum {
UIScrollViewIndicatorStyleDefault,
UIScrollViewIndicatorStyleBlack,
UIScrollViewIndicatorStyleWhite
} UIScrollViewIndicatorStyle;

Constants
UIScrollViewIndicatorStyleDefault
The default style of scroll indicator, which is black with a white border. This style is good against any content background.
UIScrollViewIndicatorStyleBlack
A style of indicator which is black and smaller than the default style. This style is good against a white content background.
UIScrollViewIndicatorStyleWhite
A style of indicator is white and smaller than the default style. This style is good against a black content background.

UIScrollView UIScrollViewIndicatorStyleDefault example.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, self.frame.size.width, self.frame.size.height-35)];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setDelegate:self];
 self.scrollView.backgroundColor = [UIColor clearColor];
 [self.scrollView setShowsHorizontalScrollIndicator:NO];
 [self.scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault];
 scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 scrollView.contentMode = UIViewContentModeScaleToFill;
 [self.scrollView setContentSize:CGSizeMake("GIVE YOUR WIDTH",0)];
[self.view addSubview:scrollView];

Example of [UIScrollView UIScrollViewIndicatorStyleDefault].
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80,320,360)];
[scrollview setContentSize:CGSizeMake(2550,360)];
[scrollview setScrollEnabled:YES];
[scrollview setPagingEnabled:YES];
[scrollview setBackgroundColor:[UIColor clearColor]];
scrollview .indicatorStyle = UIScrollViewIndicatorStyleDefault;
scrollview.showsHorizontalScrollIndicator = NO;
scrollview.showsVerticalScrollIndicator = NO;
scrollview.scrollsToTop = NO;
scrollview.delegate = self;
scrollview.tag=500;
[self.view addSubview:scrollview];

UIScrollView UIScrollViewIndicatorStyleDefault example.
[self->scrollView setScrollEnabled:YES];
[self->scrollView setDelegate:self];
self->scrollView.backgroundColor = [UIColor clearColor];
[self->scrollView setShowsHorizontalScrollIndicator:NO];
[self->scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentMode = UIViewContentModeScaleToFill;
[self->scrollView setContentSize:CGSizeMake(1300.0,0)];
[self.view addSubview:scrollView];

End of UIScrollView UIScrollViewIndicatorStyleDefault example article.

UIScrollView zoomToRect animated example in Objective C (iOS).


UIScrollView zoomToRect animated

Zooms to a specific area of the content so that it is visible in the receiver.

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

Parameters
rect
A rectangle defining an area of the content view.
animated
YES if the scrolling should be animated, NO if it should be immediate.

Discussion of [UIScrollView zoomToRect animated]
This method zooms so that the content view becomes the area defined by rect, adjusting the zoomScale as necessary.

UIScrollView zoomToRect animated example.
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // double tap zooms in
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap:) object:nil];
    float newScale = [imageScrollView zoomScale] * 1.5;
    // Note we need to get location of the tap in the imageView coords, not the imageScrollView
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:imageView]];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}

Example of [UIScrollView zoomToRect animated].
float zoomBefore = myScrollView.zoomScale;
[myScrollView zoomToRect:zoomToRect animated:YES];
float zoomAfter = myScrollView.zoomScale;
if (zoomBefore == zoomAfter)
{
    [myScrollView setContentOffset:imageCoords animated:YES];
}

UIScrollView zoomToRect animated example.
The zoomToRect method also has the argument 'animated', ie:

[self.scrollview zoomToRect:CGRectMake(0, 0, 1, IMAGE_HEIGHT) animated:NO];
So that might be causing the problem. If not, you could try this:

CGFloat scale1 = IMAGE_WIDTH / self.scrollview.frame.size.width;
CGFloat scale2 = IMAGE_HEIGHT / self.scrollview.frame.size.height;
[self.scrollview setZoomScale:MAX(scale1, scale2) animated:NO];
[self.scrollView scrollRectToVisible:CGRectMake(0,0,0,0) animated:NO];

End of UIScrollView zoomToRect animated example article.

UIScrollView zoomToRect example in Objective C (iOS).


UIScrollView zoomToRect

Zooms to a specific area of the content so that it is visible in the receiver.

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

Parameters
rect
A rectangle defining an area of the content view.
animated
YES if the scrolling should be animated, NO if it should be immediate.

Discussion of [UIScrollView zoomToRect]
This method zooms so that the content view becomes the area defined by rect, adjusting the zoomScale as necessary.

UIScrollView zoomToRect example.
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // double tap zooms in
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap:) object:nil];
    float newScale = [imageScrollView zoomScale] * 1.5;
    // Note we need to get location of the tap in the imageView coords, not the imageScrollView
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:imageView]];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}

Example of [UIScrollView zoomToRect].
float zoomBefore = myScrollView.zoomScale;
[myScrollView zoomToRect:zoomToRect animated:YES];
float zoomAfter = myScrollView.zoomScale;
if (zoomBefore == zoomAfter)
{
    [myScrollView setContentOffset:imageCoords animated:YES];
}

UIScrollView zoomToRect example.
The zoomToRect method also has the argument 'animated', ie:

[self.scrollview zoomToRect:CGRectMake(0, 0, 1, IMAGE_HEIGHT) animated:NO];
So that might be causing the problem. If not, you could try this:

CGFloat scale1 = IMAGE_WIDTH / self.scrollview.frame.size.width;
CGFloat scale2 = IMAGE_HEIGHT / self.scrollview.frame.size.height;
[self.scrollview setZoomScale:MAX(scale1, scale2) animated:NO];
[self.scrollView scrollRectToVisible:CGRectMake(0,0,0,0) animated:NO];

End of UIScrollView zoomToRect example article.

UIScrollView touchesShouldBegin withEvent inContentView example in Objective C (iOS).


UIScrollView touchesShouldBegin withEvent inContentView

Overridden by subclasses to customize the default behavior when a finger touches down in displayed content.

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

Parameters of [UIScrollView touchesShouldBegin withEvent inContentView]
touches
A set of UITouch instances that represent the touches for the starting phase of the event represented by event.
event
An object representing the event to which the touch objects in touches belong.
view
The subview in the content where the touch-down gesture occurred.

Return Value
Return NO if you don’t want the scroll view to send event messages to view. If you want view to receive those messages, return YES (the default).

Discussion of [UIScrollView touchesShouldBegin withEvent inContentView]
The default behavior of UIScrollView is to invoke the UIResponder event-handling methods of the target subview that the touches occur in.

UIScrollView touchesShouldBegin withEvent inContentView example.
- (BOOL) touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    if ([touches count] == 2) {
        return YES;
    }
    else {
        return NO;
    }
}

Example of [UIScrollView touchesShouldBegin withEvent inContentView].
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
id  element;
NSEnumerator *setEnum =   [touches objectEnumerator];

while ((element = [setEnum nextObject]) != nil)
{
    NSLog(@"element:%@", element);   
}

return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}

UIScrollView touchesShouldBegin withEvent inContentView example.
/*
 The default behavior of UIScrollView is to invoke
 the UIResponder event-handling methods of the target subview that the touches occur in.
 */
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
BOOL ret;
ret = [super touchesShouldBegin:touches withEvent:event inContentView:view];

#if TRACE_TOUCH_ENABLE
if(ret)
NSLog(@"SwTextView touchesShouldBegin,return YES");
else
NSLog(@"SwTextView touchesShouldBegin,return NO");
#endif

return ret;
}

End of UIScrollView touchesShouldBegin withEvent inContentView example article.

UIScrollView touchesShouldBegin example in Objective C (iOS).


UIScrollView touchesShouldBegin

Overridden by subclasses to customize the default behavior when a finger touches down in displayed content.

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

Parameters
touches
A set of UITouch instances that represent the touches for the starting phase of the event represented by event.
event
An object representing the event to which the touch objects in touches belong.
view
The subview in the content where the touch-down gesture occurred.

Return Value of [UIScrollView touchesShouldBegin]
Return NO if you don’t want the scroll view to send event messages to view. If you want view to receive those messages, return YES (the default).

Discussion of [UIScrollView touchesShouldBegin]
The default behavior of UIScrollView is to invoke the UIResponder event-handling methods of the target subview that the touches occur in.

UIScrollView touchesShouldBegin example.
- (BOOL) touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    if ([touches count] == 2) {
        return YES;
    }
    else {
        return NO;
    }
}

Example of [UIScrollView touchesShouldBegin].
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
id  element;
NSEnumerator *setEnum =   [touches objectEnumerator];

while ((element = [setEnum nextObject]) != nil)
{
    NSLog(@"element:%@", element);   
}

return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}

UIScrollView touchesShouldBegin example.
/*
 The default behavior of UIScrollView is to invoke
 the UIResponder event-handling methods of the target subview that the touches occur in.
 */
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
BOOL ret;
ret = [super touchesShouldBegin:touches withEvent:event inContentView:view];

#if TRACE_TOUCH_ENABLE
if(ret)
NSLog(@"SwTextView touchesShouldBegin,return YES");
else
NSLog(@"SwTextView touchesShouldBegin,return NO");
#endif

return ret;
}

End of UIScrollView touchesShouldBegin example article.

UIScrollView setZoomScale animated example in Objective C (iOS).


UIScrollView setZoomScale animated

A floating-point value that specifies the current zoom scale.

- (void)setZoomScale:(float)scale animated:(BOOL)animated

Parameters of [UIScrollView setZoomScale animated]
scale
The new value to scale the content to.
animated
YES to animate the transition to the new scale, NO to make the transition immediate.

Discussion of [UIScrollView setZoomScale animated]
The new scale value should be between the minimumZoomScale and the maximumZoomScale.

UIScrollView setZoomScale animated example.
A UIScrollView will not zoom unless it has its delegate property set to a valid UIScrollViewDelegate that responds to

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

-(void)viewDidAppear:(BOOL)animated {

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bigimage.jpg"]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.contentSize = imgView.frame.size;

    scrollView.minimumZoomScale =  scrollView.frame.size.width / scrollView.contentSize.width * 0.99;
    scrollView.maximumZoomScale =  2;

    [self.view addSubview:scrollView];
    scrollView.delegate = self;

    [scrollView addSubview:imgView];

    [scrollView setZoomScale:0.5 animated:YES];
    NSLog(@"current zoomScale: %f", scrollView.zoomScale);
    [imgView release];
}

Example of [UIScrollView setZoomScale animated].
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"bounds of current view");
    NSLog(@"widht: %f, height: %f", self.view.bounds.size.width,self.view.bounds.size.height);
    NSLog(@"size of the image");
    NSLog(@"widht: %f, height: %f", self.imageView.image.size.width,self.imageView.image.size.height);
    NSLog(@"size of the scrollView BEFORE resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
    [self.scrollView setZoomScale:0.5 animated:NO];
    NSLog(@"size of the scrollView AFTER resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
}

UIScrollView setZoomScale animated example.
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

CGSize screenSize = [[self view] bounds].size;

if (myScrollView.zoomScale <= initialZoom +0.01) //This resolves a problem with the code not working correctly when zooming all the way out.
{
imageView.frame = [[self view] bounds];
[myScrollView setZoomScale:myScrollView.zoomScale +0.01];
}

if (myScrollView.zoomScale > initialZoom)
{
if (CGImageGetWidth(temporaryImage.CGImage) > CGImageGetHeight(temporaryImage.CGImage)) //If the image is wider than tall, do the following...
{
if (screenSize.height >= CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is greater than the zoomed height of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), 368);
}
if (screenSize.height < CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is less than the zoomed height of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]);
}
}
if (CGImageGetWidth(temporaryImage.CGImage) < CGImageGetHeight(temporaryImage.CGImage)) //If the image is taller than wide, do the following...
{
CGFloat portraitHeight;
if (CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale] < 368)
{ portraitHeight = 368;}
else {portraitHeight = CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale];}

if (screenSize.width >= CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is greater than the zoomed width of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320, portraitHeight);
}
if (screenSize.width < CGImageGetWidth (temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is less than the zoomed width of the image do the following...
{
imageView.frame = CGRectMake(0, 0, CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale], portraitHeight);
}
}
[myScrollView setZoomScale:myScrollView.zoomScale -0.01];
}

End of UIScrollView setZoomScale animated example article.

UIScrollView setZoomScale example in Objective C (iOS).


UIScrollView setZoomScale

A floating-point value that specifies the current zoom scale.

- (void)setZoomScale:(float)scale animated:(BOOL)animated

Parameters of [UIScrollView setZoomScale]
scale
The new value to scale the content to.
animated
YES to animate the transition to the new scale, NO to make the transition immediate.

Discussion of [UIScrollView setZoomScale]
The new scale value should be between the minimumZoomScale and the maximumZoomScale.

UIScrollView setZoomScale example.
A UIScrollView will not zoom unless it has its delegate property set to a valid UIScrollViewDelegate that responds to

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

-(void)viewDidAppear:(BOOL)animated {

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bigimage.jpg"]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.contentSize = imgView.frame.size;

    scrollView.minimumZoomScale =  scrollView.frame.size.width / scrollView.contentSize.width * 0.99;
    scrollView.maximumZoomScale =  2;

    [self.view addSubview:scrollView];
    scrollView.delegate = self;

    [scrollView addSubview:imgView];

    [scrollView setZoomScale:0.5 animated:YES];
    NSLog(@"current zoomScale: %f", scrollView.zoomScale);
    [imgView release];
}

Example of [UIScrollView setZoomScale].
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"bounds of current view");
    NSLog(@"widht: %f, height: %f", self.view.bounds.size.width,self.view.bounds.size.height);
    NSLog(@"size of the image");
    NSLog(@"widht: %f, height: %f", self.imageView.image.size.width,self.imageView.image.size.height);
    NSLog(@"size of the scrollView BEFORE resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
    [self.scrollView setZoomScale:0.5 animated:NO];
    NSLog(@"size of the scrollView AFTER resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
}

UIScrollView setZoomScale example.
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

CGSize screenSize = [[self view] bounds].size;

if (myScrollView.zoomScale <= initialZoom +0.01) //This resolves a problem with the code not working correctly when zooming all the way out.
{
imageView.frame = [[self view] bounds];
[myScrollView setZoomScale:myScrollView.zoomScale +0.01];
}

if (myScrollView.zoomScale > initialZoom)
{
if (CGImageGetWidth(temporaryImage.CGImage) > CGImageGetHeight(temporaryImage.CGImage)) //If the image is wider than tall, do the following...
{
if (screenSize.height >= CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is greater than the zoomed height of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), 368);
}
if (screenSize.height < CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is less than the zoomed height of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]);
}
}
if (CGImageGetWidth(temporaryImage.CGImage) < CGImageGetHeight(temporaryImage.CGImage)) //If the image is taller than wide, do the following...
{
CGFloat portraitHeight;
if (CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale] < 368)
{ portraitHeight = 368;}
else {portraitHeight = CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale];}

if (screenSize.width >= CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is greater than the zoomed width of the image do the following...
{
imageView.frame = CGRectMake(0, 0, 320, portraitHeight);
}
if (screenSize.width < CGImageGetWidth (temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is less than the zoomed width of the image do the following...
{
imageView.frame = CGRectMake(0, 0, CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale], portraitHeight);
}
}
[myScrollView setZoomScale:myScrollView.zoomScale -0.01];
}

End of UIScrollView setZoomScale example article.

UIScrollView setContentOffset animated example in Objective C (iOS).


UIScrollView setContentOffset animated

Sets the offset from the content view’s origin that corresponds to the receiver’s origin.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

Parameters of [UIScrollView setContentOffset animated]
contentOffset
A point (expressed in points) that is offset from the content view’s origin.
animated
YES to animate the transition at a constant velocity to the new offset, NO to make the transition immediate.

UIScrollView setContentOffset animated example.
- (void)scrollToPage:(int)page
{
    UIScrollView *scrollView = contentView;
    CGPoint offset = CGPointMake(scrollView.bounds.size.width * page,
                                 scrollView.contentOffset.y);

    [UIView animateWithDuration:.5
                          delay:0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         [scrollView setContentOffset:offset animated:NO];
                     } completion:nil];

    [self pageControlUpdate];
}

Example of [UIScrollView setContentOffset animated].
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    svos = scrollView.contentOffset;
    CGRect rect = [textView bounds];
    rect = [textView convertRect:rect toView:self.scrollView];
    CGPoint point = rect.origin ;
    point.x = 0 ;
    [self.scrollView setContentOffset:point animated:YES];
    doneButton.enabled = YES;
}

- (IBAction)donePressed
{
    [scrollView setContentOffset:svos animated:YES];
    [textview resignFirstResponder];
    doneButton.enabled = NO;
}

UIScrollView setContentOffset animated example.
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];

End of UIScrollView setContentOffset animated example article.

UIScrollView setContentOffset example in Objective C (iOS).


UIScrollView setContentOffset

Sets the offset from the content view’s origin that corresponds to the receiver’s origin.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

Parameters of [UIScrollView setContentOffset]
contentOffset
A point (expressed in points) that is offset from the content view’s origin.
animated
YES to animate the transition at a constant velocity to the new offset, NO to make the transition immediate.

UIScrollView setContentOffset example.
- (void)scrollToPage:(int)page
{
    UIScrollView *scrollView = contentView;
    CGPoint offset = CGPointMake(scrollView.bounds.size.width * page,
                                 scrollView.contentOffset.y);

    [UIView animateWithDuration:.5
                          delay:0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         [scrollView setContentOffset:offset animated:NO];
                     } completion:nil];

    [self pageControlUpdate];
}

Example of [UIScrollView setContentOffset].
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    svos = scrollView.contentOffset;
    CGRect rect = [textView bounds];
    rect = [textView convertRect:rect toView:self.scrollView];
    CGPoint point = rect.origin ;
    point.x = 0 ;
    [self.scrollView setContentOffset:point animated:YES];
    doneButton.enabled = YES;
}

- (IBAction)donePressed
{
    [scrollView setContentOffset:svos animated:YES];
    [textview resignFirstResponder];
    doneButton.enabled = NO;
}

UIScrollView setContentOffset example.
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];

End of UIScrollView setContentOffset example article.

UIScrollView scrollRectToVisible animated example in Objective C (iOS).


UIScrollView scrollRectToVisible animated

Scrolls a specific area of the content so that it is visible in the receiver.

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

Parameters
rect
A rectangle defining an area of the content view.
animated
YES if the scrolling should be animated, NO if it should be immediate.

Discussion of [UIScrollView scrollRectToVisible animated]
This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

UIScrollView scrollRectToVisible animated example.
int page = sidePager.currentPage + 1;
CGRect frame = scroller.frame;
frame.origin.x = frame.size.width * page;

if (0 != UpAndDownPager.currentPage) {

     frame.origin.y = frame.size.height * (UpAndDownPager.currentPage + 1 );
}

scroller scrollRectToVisible:frame animated:YES];      
sidePager.currentPage = sidePager.currentPage + 1;  

Example of [UIScrollView scrollRectToVisible animated].
[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{ [scrollView scrollRectToVisible:frame animated:NO]; }
                 completion:NULL];

UIScrollView scrollRectToVisible animated example.
CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;

[myUIScrollView scrollRectToVisible:r animated:YES];

End of UIScrollView scrollRectToVisible animated example article.

UIScrollView scrollRectToVisible example in Objective C (iOS).


UIScrollView scrollRectToVisible

Scrolls a specific area of the content so that it is visible in the receiver.

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

Parameters of [UIScrollView scrollRectToVisible]
rect
A rectangle defining an area of the content view.
animated
YES if the scrolling should be animated, NO if it should be immediate.

Discussion of [UIScrollView scrollRectToVisible]
This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

UIScrollView scrollRectToVisible example.
int page = sidePager.currentPage + 1;
CGRect frame = scroller.frame;
frame.origin.x = frame.size.width * page;

if (0 != UpAndDownPager.currentPage) {

     frame.origin.y = frame.size.height * (UpAndDownPager.currentPage + 1 );
}

scroller scrollRectToVisible:frame animated:YES];      
sidePager.currentPage = sidePager.currentPage + 1;  

Example of [UIScrollView scrollRectToVisible].
[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{ [scrollView scrollRectToVisible:frame animated:NO]; }
                 completion:NULL];

UIScrollView scrollRectToVisible example.
CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;

[myUIScrollView scrollRectToVisible:r animated:YES];

End of UIScrollView scrollRectToVisible example article.

UIScrollView flashScrollIndicators example in Objective C (iOS).


UIScrollView flashScrollIndicators

Displays the scroll indicators momentarily.

- (void)flashScrollIndicators

Discussion of [UIScrollView flashScrollIndicators]
You should call this method whenever you bring the scroll view to front.

UIScrollView flashScrollIndicators example.
[myScrollView flashScrollIndicators];

Example of [UIScrollView flashScrollIndicators].
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self performSelectorOnMainThread:@selector(AdvertisementImage) withObject:self.window waitUntilDone:NO];
}

- (void) AdvertisementImage {
   
    introWeb=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 768, 1004)];
    [introWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://photos.modelmayhem.com/photos/100107/10/4b4625b0b3262.jpg"]]];
    introWeb.backgroundColor=[UIColor clearColor];
    introWeb.scalesPageToFit=YES;
    introWeb.delegate=self;
   
    for (id subview in introWeb.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
            ((UIScrollView *)subview).bounces = NO;
            [((UIScrollView *)subview) flashScrollIndicators];
        }
  
    introWeb.hidden=YES;
   
    [self.window addSubview:introWeb];
   
}

- (void) finishAdv{

   [introWeb removeFromSuperview];
  
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   
    introWeb.hidden=NO;
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(finishAdv) userInfo:nil repeats: YES];
   
}

UIScrollView flashScrollIndicators example.
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[scrollView flashScrollIndicators];
}

End of UIScrollView flashScrollIndicators example article.

Tuesday, June 4, 2013

UIScrollView zoomScale example in Objective C (iOS).


UIScrollView zoomScale

A floating-point value that specifies the current scale factor applied to the scroll view's content.

@property(nonatomic) float zoomScale

Discussion of [UIScrollView zoomScale]
This value determines how much the content is currently scaled. The default value is 1.0.

UIScrollView zoomScale example.
You should use a different UIScrollViewDelegate method.

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    // Your code here
    float scale = [scrollView zoomScale];
    [self updateAuxViewScale:scale];
}

Example of [UIScrollView zoomScale].
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //reset zoomScale back to 1 so that contentSize can be modified correctly
    self.scrollView.zoomScale = 1;

    // reset scrolling area equal to size of image
    self.scrollView.contentSize = self.imageView.image.size;

    //reset the image frame to the size of the image
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);

    //set the zoomScale to what we actually want
    self.scrollView.zoomScale = [self findZoomScale];
}

UIScrollView zoomScale example.
When you do a block animation like in your code:

[UIView animateWithDuration:4.0
                 animations:^ {
                     self.scrollView.zoomScale = 0.5;
                 }
                 completion:nil];

End of UIScrollView zoomScale example article.

UIScrollView zooming example in Objective C (iOS).


UIScrollView zooming

A Boolean value that indicates whether the content view is currently zooming in or out. (read-only)

@property(nonatomic, readonly, getter=isZooming) BOOL zooming

Discussion of [UIScrollView zooming]
The value of this property is YES if user is making a zoom gesture, otherwise it is NO .

UIScrollView zooming example.
Just create a view, add all subviews to this view and add the newly created view as a single subview to the scrollview...

Then in the viewForZoomingInScrollView delegate method return the object at Index 0:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [self.scrollView.subviews objectAtIndex:0];
}

Example of [UIScrollView zooming].
// Return the view that you want to zoom. My UIView is named contentView.
-(UIView*) viewForZoomingInScrollView:(UIScrollView*)scrollView {
  return self.contentView;
}

// Recursively find all views that need scaled to prevent blurry text
-(NSArray*)findAllViewsToScale:(UIView*)parentView {
    NSMutableArray* views = [[[NSMutableArray alloc] init] autorelease];
    for(id view in parentView.subviews) {

        // You will want to check for UITextView here. I only needed labels.
        if([view isKindOfClass:[UILabel class]]) {
            [views addObject:view];
        } else if ([view respondsToSelector:@selector(subviews)]) {
            [views addObjectsFromArray:[self findAllViewsToScale:view]];
        }
    }
    return views;
}

// Scale views when finished zooming
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGFloat contentScale = scale * [UIScreen mainScreen].scale; // Handle retina

    NSArray* labels = [self findAllViewsToScale:self.contentView];
    for(UIView* view in labels) {
        view.contentScaleFactor = contentScale;
    }
}

UIScrollView zooming example.
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
  return self.theImageView;
}

- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
  CGSize boundsSize = scroll.bounds.size;
  CGRect frameToCenter = rView.frame;
  // center horizontally
  if (frameToCenter.size.width < boundsSize.width) {
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
  }
  else {
    frameToCenter.origin.x = 0;
  }
  // center vertically
  if (frameToCenter.size.height < boundsSize.height) {
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
  }
  else {
    frameToCenter.origin.y = 0;
  }
  return frameToCenter;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
  self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView];                              
}

End of UIScrollView zooming example article.

UIScrollView zoomBouncing example in Objective C (iOS).


UIScrollView zoomBouncing

A Boolean value that indicates that zooming has exceeded the scaling limits specified for the receiver. (read-only)

@property(nonatomic, readonly, getter=isZoomBouncing) BOOL zoomBouncing

Discussion of [UIScrollView zoomBouncing]
The value of this property is YES if the scroll view is zooming back to a minimum or maximum zoom scaling value; otherwise the value is NO .

UIScrollView zoomBouncing example.
- (void)scrollViewDidEndZooming:(UIScrollView *)aScrollView withView:(UIView *)view atScale:(float)scale {
NSLog(@"scrollViewDidEndZooming: scale = %f", scale);
if (fabsf(scale - 1.0) < 1e-5) {
if (scrollView.zoomBouncing)
NSLog(@"scrollViewDidEndZooming, but zoomBouncing is still true!");

// cannot call setPagingMode now because scrollView will bounce after a call to this method, resetting contentOffset to (0, 0)
scrollViewMode = ScrollViewModeAnimatingFullZoomOut;
// however sometimes bouncing will not take place
[self performSelector:@selector(setPagingMode) withObject:nil afterDelay:0.2];
}
}

Example of [UIScrollView zoomBouncing].
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if (self.zoomBouncing && zoomedToScale / self.minimumZoomScale < 0.90) {
        // We've let go and were under 90% of the minimum size.
        self.minimumZoomScale = zoomedToScale;
        [self shrinkImageToNothing];
    } else {
        // How far have we gone?
        zoomedToScale = self.zoomScale;
    }
}

End of UIScrollView zoomBouncing example article.

UIScrollView tracking example in Objective C (iOS).


UIScrollView tracking

Returns whether the user has touched the content to initiate scrolling. (read-only)

@property(nonatomic, readonly, getter=isTracking) BOOL tracking

Discussion of [UIScrollView tracking]
The value of this property is YES if the user has touched the content view but might not have yet have started dragging it.

UIScrollView tracking example.
- (void)scrollViewDidScroll:(UIScrollView *) theScrollView {
         if (theScrollView.dragging || theScrollView.tracking)
              [self.delegate scrolling:[theScrollView contentOffSet]];
}

Example of [UIScrollView tracking].
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.isTracking){
        //do something
    }
}

End of UIScrollView tracking example article.

UIScrollView showsVerticalScrollIndicator example in Objective C (iOS).


UIScrollView showsVerticalScrollIndicator

A Boolean value that controls whether the vertical scroll indicator is visible.

@property(nonatomic) BOOL showsVerticalScrollIndicator

Discussion of [UIScrollView showsVerticalScrollIndicator]
The default value is YES. The indicator is visible while tracking is underway and fades out after tracking.

UIScrollView showsVerticalScrollIndicator example.
UIScrollView *scroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];

[scroll addSubview:detailsLabel];

Example of [UIScrollView showsVerticalScrollIndicator].
to hide the scroll indicators in an UIScrollView
[tableView setShowsHorizontalScrollIndicator:NO];
[tableView setShowsVerticalScrollIndicator:NO];

scrl.showsHorizontalScrollIndicator=NO;
scrl.showsVerticalScrollIndicator=NO;

UIScrollView showsVerticalScrollIndicator example.
- (void)viewDidLoad
{
    [super viewDidLoad];

    CPGraphHostingView *hostingView = (CPGraphHostingView *)self.view;

    UIScrollView *scroll= [[UIScrollView alloc]
initWithFrame:self.view.frame];
    self.view = scroll;
    [scroll addSubview:hostingView];
    scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3,
self.view.frame.size.height-
self.navigationController.navigationBar.frame.size.height);
    scroll.showsVerticalScrollIndicator = NO;

    graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];
    // graph configuration
-------------- 

End of UIScrollView showsVerticalScrollIndicator example article.

UIScrollView scrollsToTop example in Objective C (iOS).


UIScrollView scrollsToTop

A Boolean value that controls whether the scroll-to-top gesture is effective

@property(nonatomic) BOOL scrollsToTop

Discussion of [UIScrollView scrollsToTop]
The scroll-to-top gesture is a tap on the status bar; when this property is YES, the scroll view jumps to the top of the content when this gesture occurs. The default value of this property is YES.

This gesture works on a single visible scroll view; if there are multiple scroll views (for example, a date picker) with this property set, or if the delegate returns NO in scrollViewShouldScrollToTop:, UIScrollView ignores the request. After the scroll view scrolls to the top of the content view, it sends the delegate a scrollViewDidScrollToTop: message.

UIScrollView scrollsToTop example.

You cannot have more than one UIScrollView (or classes deriving from or using UIScrollView - i.e. UITableView) on the same UIView with the property scrollsToTop set to YES. Pick the one you want to have the feature and make sure all others are no

For example, do this:

scrollView.scrollsToTop = NO;
tableView.scrollsToTop = YES; // or not set
Implement the UIScrollView delegate method scrollViewShouldScrollToTop: and return YES if the calling UIScrollView is the UITableView.

Example of [UIScrollView scrollsToTop].
-(void)inspectViewAndSubViews:(UIView*) v level:(int)level {

NSMutableString* str = [NSMutableString string];

for (int i = 0; i < level; i++) {
    [str appendString:@"   "];
}

[str appendFormat:@"%@", [v class]];

if ([v isKindOfClass:[UITableView class]]) {
    [str appendString:@" : UITableView "];
}

if ([v isKindOfClass:[UIScrollView class]]) {
    [str appendString:@" : UIScrollView "];

    UIScrollView* scrollView = (UIScrollView*)v;
    if (scrollView.scrollsToTop) {
        [str appendString:@" >>>scrollsToTop<<<<"];
    }
}

NSLog(@"%@", str);

for (UIView* sv in [v subviews]) {
    [self inspectViewAndSubViews:sv level:level+1];
}}

UIScrollView scrollsToTop example.
- (void) ensureScrollsToTop: (UIView*) ensureView {
    ((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = NO;
}

- (void)webViewDidFinishLoad:(UIWebView *) wv {   
    [self ensureScrollsToTop: wv];
}

End of UIScrollView scrollsToTop example article.

UIScrollView scrollIndicatorInsets example in Objective C (iOS).


UIScrollView scrollIndicatorInsets

The distance the scroll indicators are inset from the edge of the scroll view.

@property(nonatomic) UIEdgeInsets scrollIndicatorInsets

Discussion of [UIScrollView scrollIndicatorInsets]
The default value is UIEdgeInsetsZero.

UIScrollView scrollIndicatorInsets example.
Actually, it is possible by changing the scrollIndicatorInsets property to restrict the indicator to a small area on the left side:

tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0,0,0,tableView.bounds.size.width-10);

Example of [UIScrollView scrollIndicatorInsets].
i.e use UIEdgeInsetMake( <top> , <left>,<bottom>,<right>); for this. i.e for your case

yourScrollView.scrollIndicatorInsets   = UIEdgeInsetMake(0, 0,bottomInset,0); //Here bottom inset would be the value you have right now .

UIScrollView scrollIndicatorInsets example.
- (void)keyboardWillShow:(NSNotification*)aNotification{
    if(!self.keyboardShown){
        NSDictionary* info = [aNotification userInfo];
       
        // Get the size of the keyboard.
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
       
        //determin the bottom inset for the table view
        UIEdgeInsets settingsTableInset = self.settingsTableView.contentInset;
        CGPoint tableViewScreenSpace = [self.settingsTableView.superview convertPoint:self.settingsTableView.frame.origin toView:nil];
        CGFloat tableViewBottomOffset = InAppSettingTableHeight-(tableViewScreenSpace.y+self.settingsTableView.frame.size.height);
        settingsTableInset.bottom = keyboardSize.height-tableViewBottomOffset;
       
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:InAppSettingKeyboardAnimation];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.settingsTableView.contentInset = settingsTableInset;
        self.settingsTableView.scrollIndicatorInsets = settingsTableInset;
        [UIView commitAnimations];
       
        self.keyboardShown = YES;
    }
}

End of UIScrollView scrollIndicatorInsets example article.