masking UIImageView when it leaves the UIViewController's frame - iphone

I have an Advertisement UIViewController class that contains the following code. The frame is the exact dimension of the image. When it animates down I want it to disappear at the 76.0 point. I made the UIViewController's frame the same size as the image too, but it doesn't obscure any of the image when it slides down.
How do I make it so that I cannot see the uiimageview when it leaves the UIViewController frame?
-(void)viewDidLoad{
UIImageView *ad = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 76.0)];
ad.image = [UIImage imageNamed:#"advertisement.jpg"];
[self.view addSubview:ad];
[ad release];
[UIView animateWithDuration:1.0 //animate the content offset
delay:6.0
options:UIViewAnimationCurveEaseIn
animations:^{
ad.frame = CGRectMake(0.0, 152.0, 320.0, 76.0); }
completion:^(BOOL finished){
}
];
}
Appdelegate:
Advertisement *theAdView = [[Advertisement alloc] init];
theAdView.view.frame = CGRectMake(0.0, self.window.frame.size.height-120.0, 320.0, 76.0);
[self.window addSubview:theAdView.view];

Set the clipsToBounds property on the view (of the viewcontroller) to YES. Subviews of the view will then be drawn only within the bounds of the view.
UIView documentation

Related

ios changing depths of views opacity transition

I'm working on something that will require me to cycle through images and create some sort of fade in and fade out transition between them. I'm used to javascript, so naturally I would want change the depth of the imageview that is going to be faded in. Is there a way to choose which view shows on the top? Or is there a better way of doing what I want to achieve?
CGRect imageFrame = CGRectMake(0.0, 0.0, 1024.0, 280.0);
UIImageView *image_one = [[UIImageView alloc] initWithFrame:imageFrame];
image_one.image = [UIImage imageNamed:#"image_one.png"];
[self.view addSubview:image_one];
UIImageView *image_two = [[UIImageView alloc] initWithFrame:imageFrame];
image_two.image = [UIImage imageNamed:#"image_two.png"];
[self.view addSubview:image_two];
You can remove a view from the view hierarchy, then re-insert it using [UIView insertSubview:aboveSubview:] and [UIView insertSubview:belowSubview:].

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.

Sliding a UIView left and right

I'm trying to animate a UIView to slide left and right. What I've tried is to take a screenshot of the current view, than replace the view with the screenshot while updating the contents of the view offscreen, then doing the animation. But the screenshot isn't showing up on the screen. It's just black until the original view slides back onto the screen. Here's the code I'm using:
UIGraphicsBeginImageContext(self.view.window.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
iv.image = image;
[self.view.window insertSubview:iv aboveSubview:self.view];
self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
iv.frame = CGRectMake(self.view.frame.size.width*2, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self loadData];
self.view.frame = CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
} completion:NULL];
[iv removeFromSuperview];
[iv release];
I found the problem. I just had to do [iv removeFromSuperview]; in the completion block. Now it's working.
Usually you need just one window in your app, the one containing all your views, so self.view.window is not a good approach, I think...
If I've got what you're trying to achieve, maybe a container view could be the right solution.
For example, supposing you are working on an iPhone:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 960.0f, 480.0f)];
firstView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
secondView.frame = CGRectMake(320.0f, 0.0f, 320.0f, 480.0f);
thirdView.frame = CGRectMake(640.0f, 0.0f, 320.0f, 480.0f);
[containerView addSubview:firstView];
[containerView addSubview:secondView];
[containerView addSubview:thirdView];
Now you have your containerView containing three views. Move your containerView left or right to show the view you like and to hide the other two views, then update the views that are out of the screen.
Another approach could be using UIScrollView as containerView.
Hope this helps...
EDIT: I've misunderstood your question, sorry.
Next try...
When you add your screenshot, you add it on the right of the screen (offscreen)
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
iv.image = image;
[self.view.window insertSubview:iv aboveSubview:self.view];
Then you move you original view on the left (offscreen)
self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
But you don't show your screenshot, so the view is blank.
I think you have to change this
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
To this
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
Or you can add an animation to show it from the right.

UIscrollview resize problem

i have a srollview with textfields and labels as its subview, for two textfields i want to show UIpickerview.
For example: when user touches textfield11 i want to show a picker that slides up from the bottom of the screen, at this point i want to change height of my scrollview too, but it is not working.
CGRect scrollframe = scrollView.frame;
NSLog(#"scrollframe.height=%f, pick height=%f",scrollframe.size.height, pick.frame.size.height);
scrollframe.size.height -= pick.frame.size.height;
[UIView beginAnimations:#"start" context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationBeginsFromCurrentState:YES];
[scrollView setFrame:scrollframe];
NSLog(#"scroll height = %f",scrollframe.size.height);
NSLog(#"scrollview height = %f", scrollView.frame.size.height);
[pick setFrame:CGRectOffset([pick frame], 0, -220)];
[UIView commitAnimations];
This is console log..
2011-06-08 10:43:31.316 AESDirect[281:207] scrollframe.height=416.000000, pick height=216.000000
2011-06-08 10:43:31.316 AESDirect[281:207] scroll height = 200.000000
2011-06-08 10:43:31.317 AESDirect[281:207] scrollview height = 200.000000
This might help you:
[scrollframe setContentOffset:CGPointMake(scrollframe.contentOffset.x, scrollframe.contentOffset.y-100)];
You can call the above code via the same function from where you are calling the pickerView. So this way you don't have to change the height of the scrollview.
Your code should be like this then:
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[contentView setBackgroundColor:[UIColor clearColor]];
self.view = contentView;
UIScrollView *horizontalScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[horizontalScroll setBackgroundColor:[UIColor blackColor]];
[horizontalScroll setDelegate:self];
[contentView addSubview:horizontalScroll];
UIPickerView *tempPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 220, 320, 100)];
[tempPicker setHidden:YES];
[contentView addSubview:tempPicker];
Make all three of them as Global in .h file and then you will have better control on their behavior. Your picker view is moving along with other items of scroll view because you might have add picker view as subview on your scroll view.
You have to change your contentsize and content offset and pickerview in view not in scroll view because if you add it in scroll view you will not be able to select values from picker view.

I have a problem with a scrollView zooming the wrong imageView

Ok so this is kind of a strange issue, but I have got two scrollViews that each have a nested imageView so as to allow for panning and zooming of an image. The tow scrollView/imageView combo's are showing up just great, and because the image is bigger than my scrollView size, I can pan just great. Here is my viewDidLoad:
-(void) viewDidLoad
{
// set the image to be displayed, pic your own image here
imageView = [[MyImage alloc] initWithImage: [UIImage imageNamed: #"newBackground.png"]];
// yes we want to allow user interaction
[imageView setUserInteractionEnabled:YES];
// set the instance of our myScrollView to use the main screen
scrollView = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// turn on scrolling
[scrollView setScrollEnabled: YES];
// set the content size to the size of the image
[scrollView setContentSize: imageView.image.size];
// add the instance of our myImageView class to the content view
[scrollView addSubview: imageView];
// flush the item
[imageView release];
// set max zoom to what suits you
[scrollView setMaximumZoomScale:1.0f];
// set min zoom to what suits you
[scrollView setMinimumZoomScale:0.25f];
// set the delegate
[scrollView setDelegate: self];
// scroll a portion of image into view (my image is very big) :)
//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];
// yes to autoresize
scrollView.autoresizesSubviews = YES;
// set the mask
scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// set the view
//self.view =scrollView;
[scrollView setFrame:CGRectMake(0, 0, 320, 230)];
[self.view addSubview:scrollView];
imageView1 = [[MyImage alloc] initWithImage: [UIImage imageNamed: #"newBackground.png"]];
// yes we want to allow user interaction
[imageView1 setUserInteractionEnabled:YES];
// set the instance of our myScrollView to use the main screen
scrollView1 = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// turn on scrolling
[scrollView1 setScrollEnabled: YES];
// set the content size to the size of the image
[scrollView1 setContentSize: imageView1.image.size];
// add the instance of our myImageView class to the content view
[scrollView1 addSubview: imageView1];
// flush the item
[imageView1 release];
// set max zoom to what suits you
[scrollView1 setMaximumZoomScale:1.0f];
// set min zoom to what suits you
[scrollView1 setMinimumZoomScale:0.25f];
// set the delegate
[scrollView1 setDelegate: self];
// scroll a portion of image into view (my image is very big) :)
//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];
// yes to autoresize
scrollView1.autoresizesSubviews = YES;
// set the mask
scrollView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// set the view
//self.view =scrollView;
[scrollView1 setFrame:CGRectMake(0,240,320,230)];
[self.view addSubview:scrollView1];
//[[self view] addSubview:scrollView];
}
No as for zooming, when I pinch to zoom on the FIRST scrollView/imageView pair, it works beautifully, no problems whatsoever. It zooms and pans no problem. BUT when I pinch to zoom my second scrollView, it pans the image of the SECOND, and zooms the image of the FIRST. SO my SECOND scrollView zooms the FIRST's image. Huh? I have no idea why that would be happening.
All I want is to have to separate images viewed that can be independently panned and zoomed.
Any thoughts as to what might be happening?
Thanks!
Your viewForZoomingInScrollView: method is probably returning the same image view for both scroll views. It needs to look at which scroll view is being passed in and return the corresponding image view.