I have three uibuttons named 1,2 and 3 and right at the bottom of those buttons i have an arrow image which indicates that which is the current button pressed.
I want that when i press any of the button the arrow starts animating and slides at the bottom of the button which is pressed.
(void)setImage:(UIImage *)image forState:(UIControlState)state;
and select UIControlState from
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
typedef NSUInteger UIControlState;
Hook all your buttons up to one method or in the method that responds to each of the button clicks call this method as well.
- (IBAction)buttonTapped:(UIButton *)sender;
{
CGPoint center = self.indicator.center;
center.x = sender.center.x;
[UIView animateWithDuration:0.25f
animations:^{
self.indicator.center = center;
}];
}
Do the following in your button pressed method:
// For Button 1
- (IBAction)pressedButton1:(id)sender{
// Create an animation lasting 1 second
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.00];
[UIView setAnimationDelegate:self];
// Set your arrow image view's center x property to that of the button
myArrowImageView.center = CGPointMake(myButton1.center.x, myArrowImageView.center.y);
[UIView commitAnimations];
}
// For Button 2
- (IBAction)pressedButton2:(id)sender{
// Create an animation lasting 1 second
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.00];
[UIView setAnimationDelegate:self];
// Set your arrow image view's center x property to that of the 2nd button
myArrowImageView.center = CGPointMake(myButton2.center.x, myArrowImageView.center.y);
[UIView commitAnimations];
}
// For Button 3
- (IBAction)pressedButton3:(id)sender{
// Create an animation lasting 1 second
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.00];
[UIView setAnimationDelegate:self];
// Set your arrow image view's center x property to that of the 3rd button
myArrowImageView.center = CGPointMake(myButton3.center.x, myArrowImageView.center.y);
[UIView commitAnimations];
}
P.S. its always good to also post some source code when you ask a question. This helps people to give you a better answer.
Related
I have a Button Add. which will add images from Gallery and Camera.
I want to show this UIVIew from ryt corner and it will expand from 1 to 2 to 3 and to final stat 4. like a baloon . and it will hide as same, from 4 to 3 to 2 to 1.
I have used this animation but this is not what i want ( balloon popover)
/*[UIView beginAnimations:#"" context:NULL];
//The new frame size
[AddImagesToCanvasView setFrame: CGRectMake(224,185,175,132)];
//The animation duration
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelay: UIViewAnimationCurveEaseIn];
[UIView commitAnimations];*/
just change the frames and radious which you want.. also add image in background if you want to display like a ballon with ballon image see code..
UIView *popUpView = [[UIView alloc] initWithFrame:view1.frame]; ///add the frame with requirement e.g view1,view2
popUpView.alpha = 0.0;
popUpView.layer.cornerRadius = 5;
popUpView.layer.borderWidth = 1.5f;
popUpView.layer.masksToBounds = YES;
[self.view addSubview:popUpView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[popUpView setAlpha:1.0];
[UIView commitAnimations];
[UIView setAnimationDuration:0.0];
UPDATE:
also see the custom popover view from bellow link..
PopupView
alpopoverview
i hope this help you...
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.
I have a button on the extreme right of the cell.
Now I have a picker view which needs to be displayed on button click.
Its working fine with the following code:
-(IBAction)buttonPressed:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
monthPicker.transform = transform;
[self.view addSubview:monthPicker];
[UIView commitAnimations];
}
But I searched for making picker view hide when it is visible and be shown when no picker view is present,I found out a solution for it.....added the following code to above method:
monthPicker.hidden = [monthPicker isHidden] ? NO : YES;
Now the picker view is not getting displayed on 1st attempt when I click button,but it's working perfect from 2nd attempt and later.
Any suggestions?
The answer from #Padavan should work, but if you want fading effect "in animation", hidden property does not do the job. You have to use UIView.alpha property instead. Here is my sample code for that.
- (void) viewDidLoad {
[super viewDidLoad];
/////////////////////////////////////////////////
// ... Initialize your month picker here //
/////////////////////////////////////////////////
monthPicker.alpha = 0;
[self.view addSubview:monthPicker];
}
- (void)buttonPressed:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
monthPicker.transform = transform;
monthPicker.alpha = monthPicker.alpha * (-1) + 1;
[UIView commitAnimations];
}
Are monthPicker initially hidden? When method called first time. If no - it hides and you can't see it. Maybe you need set monthPicker.hidden=YES in init method.
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];
}
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];