UIScrollView "back to original size" - iphone

I have a UIScrollView which has a UIView embedded in it. It scrolls up from bottom on a buttonClick, the button is located in another view. When it scrolls up it partly covers the calling view i.e view on which the button is located. Now, I have another button that is supposed to push this scroll view down. This button doesn't seem to be working. I have posted the entire code. Please Help!!
-(IBAction)unscrollClicked:(id)sender
{
//[viewController.view removeFromSuperview];
[UIView beginAnimations:#"back to original size" context:nil];
scrollView.contentSize=viewController.view.bounds.size;
//scrollView.contentSize=viewController.view.bounds.size;
[UIView commitAnimations];
}
-(IBAction)scrollClicked:(id)sender
{
//viewController.view.backgroundColor=[UIColor orangeColor];
viewController.view.frame=CGRectMake(0, 410, 320, 460);
//[self.view addSubview:viewController.view];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,40, 320, 400)];//(0,0,320,160)
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//[MyView setFrame:CGRectMake (0, 0, 320, 170)];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.contentSize = viewController.view.bounds.size;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 5.0;
scrollView.delegate = self;
[scrollView addSubview: viewController.view];
[self.view addSubview:scrollView];
[self moveScrollView:scrollView];
}
-(void) moveScrollView:(UIScrollView *)scrollView
{
CGFloat scrollamount=400;
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
}

Try using the zoomToRect method.

I think you should modify your move method. As it is, it shows content at a
fixed origin ( height 400), but after one invokation it should not do anything, since contentOffset is already at 400. Try this:
-(void) moveScrollView:(UIScrollView *)scrollView upwards:(BOOL)up {
CGFloat scrollamount=400;
if ( up)
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
else
[scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}
And invoke like this:
[self moveScrollView:scrollView upwards:YES]; // or NO for moving down

Related

iOS7 View moves under status/nav bars when loadView called again

I have a View Controller that creates its view programatically through the loadView method. The purpose of the view is to display a data models contents. When the view loads it is aligned correctly under the status and navigation bar.
But at a later point I present another view modally to allow the user to select a different data model to populate the view. This causes the the loadView to get called again and re-create the required UI for the new data model. The problem is that the view's contents now appear under the status and navigation bars!
Please note that I am not using auto-layout due to having to support iOS4 :( Also if I include the extendedLayout fix - it does fix the problem but only after the modal's dismiss animation completes leaving a jump down effect. My code below, thanks for any help.
- (void)loadView {
CGRect frame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];
CGRect scrollFrame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];
_toolbarViewController = [self createToolbarViewController];
[self.view addSubview:_toolbarViewController.view];
_toolbarViewController.productInfoWorkflowState = _productInfoWorkflowState;
UIView *containerView = [[UIView alloc] initWithFrame:scrollView.frame];
containerView.backgroundColor = [UIColor clearColor];
_headerViewController = [self createHeaderViewController];
[containerView addSubview:_headerViewController.view];
_menuViewController = [[ProductInfoMenuViewController alloc] initWithBatch:[self batchData]];
_menuViewController.delegate = self;
[containerView addSubview:_menuViewController.view];
CGRect categoriesFrame = _menuViewController.view.frame;
categoriesFrame.origin.y = _headerViewController.view.frame.size.height;
_menuViewController.view.frame = categoriesFrame;
CGRect viewFrame = containerView.frame;
viewFrame.size.height = _headerViewController.view.frame.size.height + _menuViewController.view.frame.size.height;
containerView.frame = viewFrame;
[scrollView addSubview:containerView];
scrollView.contentSize = containerView.frame.size;
_starViewController = [[StarViewController alloc] initForProduct:_productData With:[StarredItems new]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_starViewController.view];
}
Screen layout after first load:
Screen layout after second load:
With Leo's suggested fix, the scrollView is correct BUT the toolbar at the bottom now appears incorrectly. Code I used (placed after the toolbar creation code above):
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
self.automaticallyAdjustsScrollViewInsets = NO;
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
scrollView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
}
Results:
Add following code its work for me for same issue, may be its help you
/* ios 7 Change */
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
}
Does this only happen on iOS 7?
Give this a try:
self.navigationController.navigationBar.translucent = NO;
Try following:
Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
try this
CGRect frame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];
CGRect scrollFrame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];
I think the problem is due to incorrect automatic set of content insets of the scrollview.
Try the following. In your loadView, set self.automaticallyAdjustsScrollViewInsets to NO (only if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1). Now, set the contentInset and scrollIndicatorInsets to the correct values depending on OS version:
scrollview.contentInset = scrollview.scrollIndicatorInsets = UIEdgeInsetMake(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? 0 : 64, 0, 0, 0);
My problem was solved unmarking the check "Adjust Scroll View Insets" in the View Controller (using Storyboards).

Drawing Line in Two Views At a Time

I am doing an app in which i need to draw lines in one view and that automatically should appear in the other view. Any help would be appreciated..
I have tried this..
- (void)viewDidLoad
{
[super viewDidLoad];
slv = [[SmoothLineView alloc] initWithFrame:CGRectMake(DrawingView.bounds.origin.x, DrawingView.bounds.origin.y + 42, DrawingView.bounds.size.width, DrawingView.bounds.size.height - 42)];
slv.delegate = self;
[DrawingView addSubview:slv];
}
-(IBAction)btnAnotherView:(UIButton *)sender
{
[zoomingTypingView setUserInteractionEnabled:YES];
if(sender.tag == 123)
{
[self.view setBackgroundColor:[UIColor colorWithWhite:0.500 alpha:1.000]];
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 549, 768, 451);
} completion:^(BOOL finished) {
}];
CGRect imageFrame = CGRectMake(50, 50, 220, 120);
ResizableView = [[SPUserResizableView alloc] initWithFrame:imageFrame];
CGRect gripFrame = CGRectMake(50,50, 220,120);
UIView *zoom = [[UIView alloc]initWithFrame:gripFrame];
UIScrollView *zoomscroll = [[UIScrollView alloc]initWithFrame:gripFrame];
[zoomscroll setZoomScale:32.0];
[zoomscroll setMaximumZoomScale:32.0];
[zoom addSubview:zoomscroll];
[ResizableView setContentView:zoom];
ResizableView.delegate = self;
[DrawingView addSubview:ResizableView];
sender.tag = 246;
}
else
{
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 999, 768, 451);
} completion:^(BOOL finished) {
[self.view setBackgroundColor:[UIColor whiteColor]];
}];
[ResizableView removeFromSuperview];
[DrawingView addSubview:slv1];
sender.tag = 123;
}
}
In the above Image WhiteColored View is the new View and if i draw in that whiteColoredView that should reflect in both the views.
You can get more idea by looking at the NOTE TAKER HD APP.
http://www.youtube.com/watch?v=FdGDnUKZcMM
If you have two views defined:
#property(nonatomic) UIView *view1;
#property(nonatomic) UIView *view2;
instead of do self.view, you can have function does the drawing for one view:
-(void)drawView:(UIView*)view{
//your drawing code here
}
And just call the same function twice with passing different views.

How to animate view from top to bottom?

In my app I want to launch UIViewController from top to bottom.So from FirstViewController I am launching MyWebViewController(which I need to animate) as follows:
MyWebViewController *theWebViewController = [[MyWebViewController alloc] initWithFrame:self.view.bounds];
[self.view addSubview:theWebViewController.view];
and In loadView of MyWebViewController I am assigning frame to view as follows;
- (void)loadView
{
CGRect theFrame;
theFrame = CGRectMake(0.0, 20.0, mFrame.size.width, 0.0);
UIView *view = [[UIView alloc] init];
view.frame = theFrame;
self.view = view;
[view release];
}
and In viewDidLoad I am changing the frame as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[UIView beginAnimations:#"animateView" context: nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
self.view.frame = CGRectMake(0.0, 20.0, mFrame.size.width, mFrame.size.height-20.0);
[UIView commitAnimations];
}
But the animation is not happening.I even tried this code to animate from bottom to top and it is working fine but vice-versa not working.Please can anyone tell me where I am doing wrong and how I can achieve?
Thanks in advance!
I wouldn't think to set the height to 0. Instead:
- (void)loadView
{
CGRect theFrame;
theFrame = CGRectMake(0.0, -mframe.size.height + 20, mFrame.size.width, m.frame.size.height - 20);
UIView *view = [[UIView alloc] init];
view.frame = theFrame;
self.view = view;
[view release];
}
You want to set the frame above and out of the view when you add it. Then move it into place in the animation.

transition animation on only a part of the screen

I am displaying two images in my iPhone app, one on top, one on the bottom. They cover the entire screen. The user, with a swipe gesture, can change either of the images, depending on where the swipe started.
I want the image to change with an animated transition. It currently works without animation, or with the entire screen transitioning. Is it possible to make the transition occur over part of the screen?
I load the images for the first time (in viewDidLoad) thus:
// top image
UIImage *topImage = [UIImage imageNamed:#"top1.png"];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
// bottom image
UIImage *bottomImage = [UIImage imageNamed:#"bottom1.png"];
CGRect bottomframe = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f);
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:bottomframe];
bottomView.image = bottomImage;
[self.view addSubview:bottomView];
[bottomView release];
When I detect a swipe, and detect which of the two images are to be changed, I call a routine like this one:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
}
Essentilly, I'm continually loading images on top of each other. Is that the best way to do it, especially in terms of memory management?
Everything else I've tried, using techniques such as below, make the entire screen transition:
[UIView beginAnimations:nil context:nil];
...
[UIView commitAnimations];
Thanks for any leads on which way I should be going on this.
well, an easy way could be this (repeat for the bottomImage):
don't release topView when you load it the first time in your method,
but declare it in your .h file,
and use a tempView too:
#interface YourClass: UIViewController{
UIImageView *topView;
UIImageView *tempView;
}
this way you can call them to move them and remove them when you load a new image
then in .m:
EDIT: some correction (see coco's comments):
[a] [b] line 4 = [c] line 11:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
//CGRect topframe = CGRectMake(0.0f, 0.0f, (320.0f + 320), 240.0f);
CGRect topframe = CGRectMake((0.0f + 320), 0.0f, 320.0f, 240.0f);
tempView = [[UIImageView alloc] initWithFrame:topframe];
//topView.image = topImage;
tempView.image = topImage;
[self.view addSubview:tempView];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
tempView.center = topView.center;
// do this to push old topView away, comment next line if wanna new image just cover it
//topView.center = CGPointMake(topView.x - 320, topView.y);
topView.center = CGPointMake(topView.center.x - 320, topView.center.y);
// call a method when animation has finished:
[UIView setAnimationDidStopSelector:#selector(endOfAnimation:finished:context:)];
[UIView commitAnimations];
}
- (void)endOfAnimation:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[topView removeFromSuperview];
topView = tempView;
[tempView release];
tempView = nil;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (void)dealloc {
if (tempView != nil) {
[tempView release];
}
[topView release];
[super dealloc];
}

UIScrollView "scroll back"

I have a UIScrollView which has a UIView embedded in it. It have 2 buttons "scroll" and "unscroll".On clicking the "scroll" button, my scrollview scroll up from the bottom of the parent view. Everything works fine until here but when I click the "unscroll" button to push the scrollview back to where it came from, nothing happens. I have posted the entire code here. Please check where the fault lies !!. Already spent a sizeable amount of time on it.
-(IBAction)unscrollClicked:(id)sender
{
//[viewController.view removeFromSuperview];
[UIView beginAnimations:#"back to original size" context:nil];
scrollView.contentSize=viewController.view.bounds.size;
//scrollView.contentSize=viewController.view.bounds.size;
[UIView commitAnimations];
}
-(IBAction)scrollClicked:(id)sender
{
//viewController.view.backgroundColor=[UIColor orangeColor];
viewController.view.frame=CGRectMake(0, 410, 320, 460);
//[self.view addSubview:viewController.view];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,40, 320, 400)];//(0,0,320,160)
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//[MyView setFrame:CGRectMake (0, 0, 320, 170)];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.contentSize = viewController.view.bounds.size;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 5.0;
scrollView.delegate = self;
[scrollView addSubview: viewController.view];
[self.view addSubview:scrollView];
[self moveScrollView:scrollView];
}
-(void) moveScrollView:(UIScrollView *)scrollView
{
CGFloat scrollamount=400;
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
}
1st problem i see with this code is that you initialize scrollView in scrollClicked and never store pointer to it, so in unscrollClicked pointer should be nil.
Another problem is that as far as i know, animation is conducted this way:
[UIView beginAnimations:#"Animate" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationBeginsFromCurrentState:YES];
[somView setFrame:CGRectMake(100, 100, 100, 100)];
[UIView commitAnimations];