how i can move label over all view - iphone

My view layout http://i.imgur.com/PkZ9P.png
-(IBAction)tapview:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1];
CGRect frame = labelgreen.frame;
frame.origin.y -= 200;
labelgreen.frame = frame;
[UIView setAnimationDidStopSelector:#selector(forview1)];
[UIView commitAnimations]; }
but when label move it move only inside view1 or view2 or view 3,
i would like to let it move over all view to color view
what i should do?
and after animation finish it should run function -(void)forview1
but the function is not call
-(void)forview1
{colorview.backgroundcolor=changecolor;}
thank you

Spelling mistake in ur function
NOT
[UIView setAnimationDidStopSelector:#selector(foeview1)];
//correct
[UIView setAnimationDidStopSelector:#selector(forview1)];
more over
replace this line
labelgreen.frame.frame = frame;
To
labelgreen.frame = frame;
declare this function in ur .h
-(void)forview1;

First, try to replace
[UIView setAnimationDidStopSelector:#selector(foeview1)];
with
[UIView setAnimationDidStopSelector:#selector(forview1)];
Second, push label on the top of views using following method
- (void)bringSubviewToFront:(UIView *)view
For example:
UILabel * labelgreen;
[labelgreen.superview bringSubviewToFront:labelgreen];
If you are creating this view in IB then just drag UILabel to the bottom of its subviews.

You should use block based animations. Those functions are deprecated.
[UIView animateWithDuration:animationDuration animations:^{
}];

The label views are nested inside View1/View2/View3 and thus are clipped to the bounds of those containing views. To make them move around the outer view you will need to either start them off in the main view, or before starting the animation remove them from the containing views and add them to the outer view. You could also look into whether the clipToBounds property can be used to have the label views render when they are moved out of the bounds of their containing view.
Example of removing and re-adding in containing view (assuming the labelgreen view is inside a view called view2) (do this before you start your animation).
CGRect newframe = labelgreen.frame
newframe.origin.y = newframe + view2.frame.origin.y;
newframe.origin.x = newframe + view2.frame.origin.x;
[labelgreen removeFromSuperview];
labelgreen.frame = newframe;
Then either:
[colorview addSubview:labelgreen]; // (assuming colorview is your main containing view).
Or:
[self.view addSubview:labelgreen]; // if self.view is the main containing view

Related

How to add a subview with drag in animation from top of the view, in iOS?

I need to add a UIDatePicker subview on a button click to my main view.
How can I add the view with animation such that the subview flips in from the top of the main view and on removing ,undergoes the reverse animation ? I know the subview can be added in a frame that is outside the visible area of the view and then moving it to visible area using CABasicAnimation will be abetter choice. So need help regarding the animation block that I should use.
Thanks in advance
Try this bellow code... here if yourView default frame with origin y = -500 then try bellow code on any UIButton click event.. also set frame which you want
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
CGRect frame = yourView.frame;
frame.origin.y = frame.origin.y + 500; // give any value for display yourView
yourView.frame = frame;
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourView cache:YES];
[UIView commitAnimations];
Use CATransaction for animation.

Unable to animate just a frame to slide up on click of a button.

I am try to create an animation in my app which would slide up a frame to my desired position. The frame would be having other buttons and text on it as well.
I was unable to use actionsheet for this, as it starts from the bottom most part of the window. What i want to create is shown in the picture below.
the frame I want to animate is the one with Capture mode: text.
Click on the button on the tabView I want this frame to slide up, starting from the top of the tabview. and again clicking the button should make the frame disappear.
So far my code looks like
-(IBAction)setting
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor clearColor]];
[tempView setAlpha:.87];
tempView.hidden = YES;
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem I am facing here is that when I animate my frame to move, the main view moves along with it. Which is something I don't want it to do.
I am new to iPhone programming, please guide how can I fix this.
Or what would be a better way to do it.
Thanks
You don't need to make the view hidden, just add it as a subview after you set its frame to have y origin of 480. Also, don't use the old UIView animation methods; use blocks.
Here's an example of a view that starts with a y origin of 0. It gets positioned at negative y equal to its height, added as a subview then animated down to meet the keyboard coming up from the bottom of the screen.
// add to view while completely off-screen
CGRect unlockViewFrame = unlockView.frame;
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
unlockView.frame = unlockViewFrame;
[self.view addSubview:unlockView];
unlockViewFrame.origin.y = 0.;
[UIView animateWithDuration:0.3 animations:^{
unlockView.frame = unlockViewFrame;
}];
Here's the same thing in reverse, using a completion block to remove the view.
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
[UIView animateWithDuration:0.3 animations:^{
view.frame = unlockViewFrame;
} completion:^(BOOL completion) {
[view removeFromSuperview];
}];
Another thing I notice is that you are doing everything in code; consider doing it in Interface Builder. A smart man once told me that code you don't write can't have any bugs and he was right. It's easy to create a view in IB and instantiate it inyour code when you need it:
NSArray* nibViews;
nibViews = [[NSBundle mainBundle] loadNibNamed:#"UnlockView" owner:self options:nil];
UnlockView *unlockView = [nibViews objectAtIndex:0];
Also, consider using ARC. No more autorelease!
Your resetting frame of main view and not tempView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
tempView.frame=tmpFrame; //it should be tempView not self.view ie main view
[UIView commitAnimations];

How to add a view behind other view in iOS

I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.
This is my code:
- (void)handlePressedButton {
if (pressedButton.selected) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
pressedButton.selected = NO;
}
else {
pressedButton.selected = YES;
if (leftPanelView) {
[leftPanelView removeFromSuperview];
leftPanelView = nil;
}
CGRect viewFrame = CGRectMake(-196, 0, 236, 748);
leftPanelView = [[UIView alloc] initWithFrame:viewFrame];
leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"left-nav-content.png"]];
// Code to populate leftPanelView according to what button is pressed.
[self populateLeftPanelView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
}
}
Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)
Besides addSubview, there are other methods you can rely on to add your view:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:
You can control the index (actually z-index, or layer order) of the view using these methods.
try sendSubviewToBack
[self.view sendSubviewToBack:leftPanelView];
Swift 5
Insert a view below another view in the view hierarchy:
view.insertSubview(subview1, belowSubview: subview2)
Insert a view above another view in the view hierarchy:
view.insertSubview(subview1, aboveSubview: subview2)
Insert a subview at the specified index:
view.insertSubview(subview, at: index)
Move the specified subview so that it appears behind its siblings:
subview1.sendSubviewToBack(subview2)
Move the specified subview so that it appears on top of its siblings:
subview1.bringSubviewToFront(subview2)
Exchange the subviews at the specified indices:
view.exchangeSubview(at: index1, withSubviewAt: index2)
Oh, I found a solution myself. I added after this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
this line:
[self.view sendSubviewToBack:leftPanelView];
In Swift 5
self.view.sendSubviewToBack(leftPanelView)
Another nice idea is to give the Subviews a tag.
Swift
mainView.addSubView(topView)
mainView.addSubView(bottomView)
topView.tag = 2
mainView.insertSubview(bottomView, belowSubview: mainView.viewWithTag(2))
Objective-C
Insert a view below another view in the view hierarchy:
- (void)insertSubview:(UIView *)subview1
belowSubview:(UIView *)subview2;
Insert a view above another view in the view hierarchy:
- (void)insertSubview:(UIView *)subview1
aboveSubview:(UIView *)subview2;
Insert a subview at the specified index:
- (void)insertSubview:(UIView *)view
atIndex:(NSInteger)index;
Move the specified subview so that it appears behind its siblings:
- (void)sendSubviewToBack:(UIView *)view;
Move the specified subview so that it appears on top of its siblings:
- (void)bringSubviewToFront:(UIView *)view;
Exchange the subviews at the specified indices:
- (void)exchangeSubviewAtIndex:(NSInteger)index1
withSubviewAtIndex:(NSInteger)index2;

Make textfield appear from above in an animated way

Is it possible to make textfield come from above to down in an animated way when a button is pressed?
Yes. You must chnage the frame of the UITextField inside an animation block. Something like this. Assuming, for example, that the Y origin of the text field before pressing the button is 0.0f (top of its container).
- (void) buttonPressed {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: <desired_animation_duration_in_seconds> ];
CGRect aFrame = _myTextField.frame;
aFrame.origin.y = <desired_y_coord>;
_myTextField.frame = aFrame;
[UIView commitAnimations];
}

iPhone UIView Question. How to get notified when a UIView becomes completely hidden?

I have a have a strip of UIViews that slides horizontally behind a UIView "window". Only the UIViews within the bounds of the "window" are seen. As a view becomes hidden I would like to be notified so that I can perform some task with the just hidden view. What is the best way to do this?
In your UIScrollViewDelegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// left and right bounds of the subview in relation to the scrollview
CGFloat left = mySubview.frame.origin.x - myScrollView.contentOffset.x;
CGFloat right = left+mySubview.frame.size.width - myScrollView.contentOffset.x;
if(right<=0 || left>=myScrollView.frame.size.width){
// The view is not visible
}
}
this checks if either the left or right side of the subview is visible.
Add a callback selector to your animation:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:theView cache:NO];
[UIView setAnimationDidStopSelector:#selector(animationDone)];
theView.frame = newFrame;
[UIView commitAnimations];