`
[self lastbuttonOnToThisPosition:selectedbtn.tag]
-(int)lastbutt0nToThisPosition:(int)pos
{
int p=[array count]-1;
if((pos) == ([array count]-1))
return 0;
for(p=[array count]-1;p>=0;--p)
{
UIButton *btn=(UIButton*)[self.view viewWithTag:p];
if(![btn isSelected])
{
UIButton *Remove=(UIButton*)[self.view viewWithTag:pos];
[Remove removeFromSuperview];
[btn setTag:pos];
[array replaceObjectAtIndex:pos withObject:[array objectAtIndex:p]];
[array removeObjectAtIndex:p];
return p;
}
}
return p;
}`
I have say for example N UIButtons arranged sequentially, once I select any of them it gets removed. I want to animate the N-1th button to the position where other buttons removed.
Please can anyone suggest me suitable method.
Because of you didnt provide your code, i am including a sample code. Please make changes according to your need.
If there are 3 buttons and if you are tapping second button, then our method should be
-(IBAction)tappedBtn2:(id)sender
{
[UIView animateWithDuration:0.33f
delay:0.0f
options:UIViewAnimationOptionShowHideTransitionViews
animations:^{
[btn2 removeFromSuperview];
[btn3 removeFromSuperview];
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.33f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[btn1 setFrame:CGRectMake(btn2.frame.origin.x, btn2.frame.origin.y , btn1.frame.size.width, btn1.frame.size.height)];
}
completion:nil];
}];
}
If you are using a for loop, then use the below code for obtaining the uibutton with a particular tag for deletion and animation
[(UIButton*)[self.view viewWithTag:tagId] // assuming buttons are added to self.view
Related
I have a view controller that have a scroll view that bounce horizontally , and in this scroll view I have a label.
I can now hold the label and scroll it down, and if I release it will bounce up back.
What I want is that: When I scroll the view y coordinate (using myScrollView.contentOffset.y) to some value, lets say -33 and under I can release my fine and the label will animate to the bottom of the screen and disappear, and now I can set the label to be a new value, and It will animate from top to the label original position.
Here a photo of how the view controller looks like:
And this is the relevant method I already implemented (powered by #rebello95):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.myScrollView.contentOffset.y <= -73) {
[UIView animateWithDuration:0.3 animations:^{
self.homeLabel.alpha = 0.0;
} completion:^(BOOL finished) {
[self.homeLabel removeFromSuperview];
self.homeLabel = nil;
}];
}
NSLog(#"%f", self.myScrollView.contentOffset.y);
}
Now I want it to slide to the bottom of the page and fade.
thanks!
EDIT: The animation now moves the label to the bottom of the view controller then fades it out.
You can use an animation block to move the label, then put another block inside the completion block to fade out the label, then remove it after the animation completes.
Example:
[UIView animateWithDuration:0.3 animations:^{
[self.myLabel setFrame:CGRectMake(self.myLabel.frame.origin.x, self.view.frame.size.height - self.myLabel.frame.size.height, self.myLabel.frame.size.width, self.myLabel.frame.size.height)];
self.labelRemoving = YES;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
self.myLabel.alpha = 0.0;
} completion:^(BOOL finished) {
[self.myLabel removeFromSuperview];
self.myLabel = nil;
self.labelRemoving = NO;
}];
}];
Sidenote: You should probably be using <= instead of == in your if statement to achieve your desired results. In addition, you may want to set a flag to indicate that your label is being removed (since that method will inevitably be called multiple times). Something like this:
//.h
#property (nonatomic, assign) BOOL labelRemoving;
//.m
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.myScrollView.contentOffset.y <= -33 && !self.labelRemoving) {
[UIView animateWithDuration:0.3 animations:^{
[self.myLabel setFrame:CGRectMake(self.myLabel.frame.origin.x, self.view.frame.size.height - self.myLabel.frame.size.height, self.myLabel.frame.size.width, self.myLabel.frame.size.height)];
self.labelRemoving = YES;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
self.myLabel.alpha = 0.0;
} completion:^(BOOL finished) {
[self.myLabel removeFromSuperview];
self.myLabel = nil;
self.labelRemoving = NO;
}];
}];
}
NSLog(#"%f", self.myScrollView.contentOffset.y);
}
I am having the code below, which is an animation of a UIButton. But when the button is pressed, i want the animation stop at once and do something else, while if the animation continue it does something else. My problem is that even i press the button, the animation continue and it will do both step 1 and 2.
What am i missing in the code?
int frame1 = 600;
int frame2 = 100;
hit11 = [[UIButton alloc]initWithFrame:CGRectMake((arc4random() % (frame1 - frame2)+frame2),200,20,20)];
[hit11 addTarget:self action:#selector(hit:) forControlEvents:UIControlEventTouchUpInside];
[hit11 setBackgroundImage:[UIImage imageNamed:#"touchrightdown.png"] forState:UIControlStateNormal];
[self.view addSubview:hit11];
[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{
hit11.transform = CGAffineTransformMakeScale(7,7);
hit11.alpha=0.5;
} completion:^(BOOL finished) {
if (finished) {
//DO STEP 2.
hit11.hidden=YES;
}
NSLog(#"got hit");
}];
-(void) hit:(UIButton*)sender{
NSLog(#"1/10");
//DO STEP 1.
}
Try adding this:
[sender.layer removeAllAnimations];
in your hit:(UIButton*)sender method.
There's an array with uiviews. Their frames are set due to current interfaceOrientation
Then we add these views to self.view programmatically using
-(void)placeView
{
if ([self.arrayWithViews count] > 0)
{
[UIView animateWithDuration:1.f animations:^{
UIView *currentView = [self.arrayWithViews lastObject];
[currentView setAlpha:0.f];
[self.view addSubview:currentView];
[currentView setAlpha:1.f];
[self.arrayWithViews removeLastObject];
} completion:^(BOOL finished) {
[self placeView];
}];
}
}
Now, if the orientation change happens the views keep adding (with coordinates corresponding to previous orientation).
The question is - how to stop the animation process or how to prevent interface orientation change while the animation takes place.
P.S. [self.view.layer removeAllAnimations] doesn't work (I didn't figure out why)
Could you please advise what to do?
removeAllAnimations will still call completion block, so you repeat to call placeView, so you need add a flag:
-(void)placeView
{
if(!self.runAni){ //Add a flag to stop repeat to call [self placeView]
return;
}
if ([self.arrayWithViews count] > 0)
{
[UIView animateWithDuration:1.f animations:^{
UIView *currentView = [self.arrayWithViews lastObject];
[currentView setAlpha:0.f];
[self.view addSubview:currentView];
[currentView setAlpha:1.f];
[self.arrayWithViews removeLastObject];
} completion:^(BOOL finished) {
[self placeView];
}];
}
}
to stop animation, you should do :
self.runAni = NO;
[self.view.layer removeAllAnimations];
begin to do:
self.runAni = YES;
[self placeView];
I have a View (CupsViewController) with a Label and when it's clicked, TableView (AddressTableController) slides from the bottom of the screen and stays in the lower half.
In TableView, when Ok Buttonis pressed, I want to change value of Labeland remove TableViewwith animation (that is, slide to the bottom).
Here is my code:
CupsViewController
Method called when click on Label
- (IBAction)showPop:(id)sender
{
addressTableController = [[AddressTableController alloc]initWithStyle:UITableViewStyleGrouped];
[addressTableController setDelegate:self];
[[self view] addSubview:[addressTableController myTableView]];
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[[addressTableController myTableView] setFrame:CGRectMake(0, kScreenHeight * 0.5, kScreenWidth, kScreenHeight * 0.5)];
} completion:nil];
}
Method called from AddressTableControllerwhen Buttonis pressed
- (void)addressTableController:(AddressTableController *)viewController didChooseValue:(int)value {
direccionLabel.text = [[[[myAppDelegate usuarioActual] cups] objectAtIndex:value] direccion];
[addressTableController.view removeFromSuperview];
}
Image
As you can see, I've tried with removeFromSuperviewbut it does nothing. How can I slide TableViewto the bottom?
It seems that you myTableView property returns a different view, not the one that is referenced in the view property. So you basically add the former as a subview ([[self view] addSubview:[addressTableController myTableView]];), but try to remove the latter ([addressTableController.view removeFromSuperview];).
Just do
[[addressTableController myTableView] removeFromSuperview];
instead of
[addressTableController.view removeFromSuperview];
Here you go. Cheers, mate! :)
P.S.
if you want to animate the view out, you can do it like this
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
// animation code...
} completion:^(BOOL finished) {
[[addressTableController myTableView] removeFromSuperview];
}];
Hello guys I am new to xcode and I am trying to animate balloons on touch by changing its images: this is my code:
now the problem i am facing is its not animating images means the animation timer is not working: please guide me what should i do to animate the images with time: if I am not doing it properly then please guide me that how can i do it by NSTimer?
-(void)baloonbursting:(UIButton *)button withEvent:(UIEvent *)event{
if ([[UIImage imageNamed:#"redbaloons.png"] isEqual:button.currentImage]) {
NSLog(#"em redbaloons.png");
UIImage *bubbleImage3 = [UIImage imageNamed:#"redburst.png"];
[button setImage:bubbleImage3 forState:UIControlStateNormal];
}
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:1.0f animations:^(){
// define animation
if ([[UIImage imageNamed:#"redburst.png"] isEqual:button.currentImage]) {
NSLog(#"em redbaloons.png");
UIImage *bubbleImage3 = [UIImage imageNamed:#"redburst2.png"];
[button setImage:bubbleImage3 forState:UIControlStateNormal];
}
}
completion:^(BOOL finished){
// after the animation is completed call showAnimation again
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction animations:^{
} completion:^(BOOL finished){
if (finished) {
[button removeFromSuperview];
}}];
}];
}
I want you to give you a solution to show you the right direction to think. That is, why I developed a small test project in Xcode and I controlled that this code really works.
First: forget NSTimers for this animation!
The idea is, that you "play" around with subviews, because you can change their alpha properties from 0.0 (invisible) to 1.0 (fully opaque), which are supported by the SDK's view animations.
Please change the image names according to your own file names (I've used my own here).
The following method checks, if the button's image is that one that shall invoke an animation - exactly what you did before. If this condition is met, it visually animates the change of the button's image to another image:
- (IBAction)balloonBursting:(UIButton *)sender
{
BOOL doAnimate = NO;
UIImageView *ivOldBubbleImage;
UIImageView *ivNewBubbleImage;
if ([[UIImage imageNamed:#"BalloonYellow.png"] isEqual:sender.currentImage]) {
NSLog(#"will animate");
doAnimate = YES;
UIImage *newImage = [UIImage imageNamed:#"BalloonPurple.png"];
ivNewBubbleImage = [[UIImageView alloc] initWithImage:newImage];
ivNewBubbleImage.alpha = 0.0;
ivOldBubbleImage = sender.imageView;
[sender addSubview:ivNewBubbleImage];
}
if (doAnimate) {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:1.0f animations:^(){
// define animation
ivOldBubbleImage.alpha = 0.0;
ivNewBubbleImage.alpha = 1.0;
}
completion:^(BOOL finished){
[sender setImage:ivNewBubbleImage.image forState:UIControlStateNormal];
[ivNewBubbleImage removeFromSuperview];
}];
}
}