UILabels Marquee + Overlapping while animating labels - iphone

I wrote following code for adding marquee effect on labels being shown one bye one on a view.
- (void)marqueeMessage:(NSString *)messageString
{
UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(520, 0, 480, 21))];
label.text = messageString;
[self.view addSubview:label];
[UIView animateWithDuration:0.2
animations:^ {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:20];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
label.frame = CGRectMake(-480, 0, 480, 21);
[UIView commitAnimations];
}
completion:^(BOOL finished) {
NSLog(#"Animation Done!");
if (array.count > 0)
{
nextIndex++;
NSString *strMessage = [array objectAtIndex:nextIndex];
[self marqueeMessage:strMessage];
}
}];
}
Some how, strings in array are being displayed in such a manner, that they are overlapping while performing animation.
Any idea, anyone???
Let me know, in case need more information.

-(void)viewDidLoad {
[super viewDidLoad];
array=[[NSArray alloc]initWithObjects:#"Scroll test",#"Scroll test1",#"Scroll test2",#"Scroll test3",#"Scroll test4",nil];
[self marqueeMessage:[array objectAtIndex:0]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)marqueeMessage:(NSString *)messageString {
label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))];
//label.tag=nextIndex;
label.text = messageString;
[self.view addSubview:label];
[UIView beginAnimations:#"LBL" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDidStopSelector:#selector(performThis:)];
[UIView setAnimationDelegate:self];
label.frame = CGRectMake(360,50,90,21);
[UIView commitAnimations];
}
- (void) performThis:(id) sender {
if (i<[array count]) {
label.text = [array objectAtIndex:i];
i++;
}
else
{
i=0;
label.text = [array objectAtIndex:i];
}
label.frame = CGRectMake(-90,50,90,21);
[UIView beginAnimations:#"LBL" context:nil];
[UIView setAnimationDuration:3];
label.frame = CGRectMake(360,50,90,21);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(performThis:)];
[UIView commitAnimations];
}
may help you.

I think you're never removing labels from superview... you need to keep reference to your label and do so :
[old_label removeFromSuperview];

It looks to me that your first animation (which lasts 0.2 seconds) finishes and the completion is called, which adds another label to your view, on top of the nested animation which lasts for 20 seconds and is therefore still on the screen, etc, etc. You'll end up with a pile of overlapping labels.

Related

Inserting a subview and changing position of another view at the same method

I am trying to add a subview (UITextField) to a view and change the frame property of a UITextView in the same method but it's not working as I was expecting. This is the code:
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView commitAnimations];
[self.view insertSubview:textField aboveSubview:self.tvContent];
Note: tvContent is the UITextView object which I am trying to reposition.
The problem I am facing is that the UITextView isn't moving at all IF the last line of code (insertSubview) is in place. The UITextField does show up though. However, if I remove the last line , the textview change its position just fine.
I have also tried without animation by just setting the frame property of self.tvContent and same behavior occurred.
Any ideas to fix this?
Do like this,
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView setAnimationDidStopSelector:#selector(animationFinish)];
[UIView commitAnimations];
-(void)animationFinish {
[self.view insertSubview:textField aboveSubview:self.tvContent];
}
Is there a reason you are still using the good ol' beginAnimations thing?
This would do the same:
[UIView animateWithDuration:0.5 animations:^(void) {
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
} completion:^(BOOL finished) {
[self.view addSubview:textField];
}];
EDIT: What happens if you just addSubView it?

UIAnimation - Show UIView from button.frame.origin.y

I have a UIButton somewhere on my view. On touch event of button I making a UIView appear. The UIAnimation I have used make the view appear from top of window. But I want it to appear from button.frame.origin.y . Before touching the button the view is not visible. On touching the button view should start appearing from above position.
Here is the code :
-(IBAction) ShowInfowView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = rect.size.height - rect.size.height+58.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
This is how I am hiding the view again :
-(IBAction) HideInfoView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
In viewDidLoad I am doing the following :
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
viewInfo.frame = rect;
UPDATE:
Please see this example. Here view is appearing from top of screen. I want it to appear from button y axis. For that matter please consider button y position a bit upwards.
So you want a slide-in effect, but not from the top of the screen, just some arbitrary value?
One way to do it:
1) You should create a view that has the dimensions and position of your desired view AFTER animation finishes, we'll call it baseview.
2) Set this baseview property clipsToBounds to YES. This will make all subviews that are outside of the baseview's frame invisible.
3) Add your animating view as a subview of the baseview, but set the frame so it is invisible (by plcacing it above the baseview):
frame = CGRectMake(0, -AnimViewHeight, AnimViewWidth, AnimViewHeight);
4) Animate the animview frame:
//put this inside of an animation block
AnimView.frame = CGRectMake(0, 0, AnimViewWidth, AnimViewHeight);
EDIT:
Example:
//define the tags so we can easily access the views later
#define BASEVIEW_TAG 100
#define INFOVIEW_TAG 101
- (void) showInfo
{
//replace the following lines with preparing your real info view
UIView * infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[infoView setBackgroundColor: [UIColor yellowColor]];
[infoView setTag: INFOVIEW_TAG];
//this is the frame of the info view AFTER the animation finishes (again, replace with real values)
CGRect targetframe = CGRectMake(100, 100, 100, 100);
//create an invisible baseview
UIView * baseview = [[UIView alloc] initWithFrame:targetframe];
[baseview setBackgroundColor: [UIColor clearColor]];
[baseview setClipsToBounds: YES]; //so it cuts everything outside of its bounds
[baseview setTag: BASEVIEW_TAG];
//add the nfoview to the baseview, and set it above the bounds frame
[baseview addSubview: infoView];
[infoView setFrame:CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height)];
//add the baseview to the main view
[self.view addSubview: baseview];
//slide the view in
[UIView animateWithDuration: 1.0 animations:^{
[infoView setFrame: baseview.bounds];
}];
//if not using ARC, release the views
[infoview release];
[baseview release];
}
- (void) hideinfo
{
//get the views
UIView * baseView = [self.view viewWithTag: BASEVIEW_TAG];
UIView * infoView = [baseView viewWithTag: INFOVIEW_TAG];
//target frame for infoview - slide up
CGRect out_frame = CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height);
//animate slide out
[UIView animateWithDuration:1.0
animations:^{
[infoView setFrame: out_frame];
} completion:^(BOOL finished) {
[baseView removeFromSuperview];
}];
}
I have done exactly what you're trying in my recent project.
The following steps should make this work:
1. In your viewDidLoad: you init your viewToFadeIn like this:
viewToFadeIn = [[UIView alloc] initWithFrame:CGRectMake(20,self.view.frame.size.height+10, self.view.frame.size.widh-40, 200)];
//its important to initalize it outside your view
//your customization of this view
[self.view addSubview:viewToFadeIn];
2.your ShowInfowView: Method should look like this:
` -(IBAction) ShowInfowView:(id)sender{
[UIView beginAnimations:#"fadeIn" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, -290);
//290 is in my case, try a little for your correct value
[UIView commitAnimations];
}
3.yourHideInfoView:` Method looks like this
-(IBAction) HideInfoView:(id)sender{
[UIView beginAnimations:#"fadeOut" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
}
EDIT:
4. animationFinishedMethod:
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
if ([animationID isEqual:#"fadeIn"]) {
//Do stuff
}
if ([animationID isEqual:#"fadeOut"]) {
//Do stuff
}
}
SEE THIS
This should do the trick. Good luck
Your problem is not clear (for me).
What is this?
rect.origin.y = rect.size.height - rect.size.height+58.0;
Is 58 origin of your UIButton?
You should use sender.frame etc
Use blocks to animate like this
[UIView animateWithDuration:0.5f animations:^{
// Animation here
} completion:^(BOOL finished) {
// onComplete
}];
- (void) slideIn{
//This assumes the view and the button are in the same superview, if they're not, you'll need to convert the rects
//I'm calling the animated view: view, and the button: button...easy enough
view.clipToBounds = YES;
CGRect buttonRect = button.frame;
CGRect viewRect = CGRectMake(buttonRect.origin.x, buttonRect.origin.y + buttonRect.size.height, view.bounds.size.width, 0);
CGFloat intendedViewHeight = view.height;
view.frame = viewRect;
viewRect.size.height = intendedViewHeight;
[UIView animateWithDuration:.75 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
view.frame = viewRect;
}completion:nil];
}
This will cause the the view to appear to slide out from under the button. To slide the view back in, you just reverse the animations. If you wish to slide it up from the button, you'll need to make sure your starting 'y' is the 'y' value of the button (rather than the 'y' + 'height') and animate both the height and y values of the view that needs animating.

Move UIButton 10px down and open new ViewController

I'm newbie at iOS development and this is my first question on SO.
I want to create "animation".
When I tap UIButton I want to slowely (about 0.5 seconds) move it down for 10px and when I raise my finger I want to open new viewController.
I made something but I don't know how to make "animation".
UIImage *normalImage = [UIImage imageNamed:#"FirstImage"];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[firstButton setImage:normalImage forState:UIControlStateNormal];
[firstButton addTarget:self action:#selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
- (void)showSecondViewController; {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:secondViewController animated:YES];
[progressViewController release];
}
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void) {
//put your animation result here
//then it will do animation for you
} completion:^(BOOL finished){
//do what you need to do after animation complete
}];
for example:
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//add firstButton to some view~
//....
firstButton.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^(void) {
firstButton.transform = CGAffineTransformMakeTranslation(0,10);
} completion:^(BOOL finished){
show your view controller~
}];
[UIView animateWithDuration:0.5 animations: ^ {
button.frame = CGRectMake:(button.frame.origin.x, button.frame.origin.y + 10.0f, button.frame.size.width, button.frame.size.height);
}];
You can put transformations(repositioning), scaling and rotatations between beginAnimations and commitAnimations blocks where you can specify the AnimationRepeatCount,AnimationCurve etc.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
..........................//etc.
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[UIView commitAnimations];
But in iOS4 and later animations are encouraged to be performed by
animateWithDuration:delay:options:animations:compeletion and similar methods that invlove blocks which are very powerful. For more information on block animations please refer to apple documentation

How to animate a UITextView that is a subview?

I am trying to animate a UITextView (myTextView) that is a subview inside a UIScrollView (scrollView).
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:myTextView cache:YES];
[scrollView addSubview:myTextView];
[UIView commitAnimations];
myTextView appears without animation!
What kind of animation do you want to do? If you want to fade in, for example, you have to do
[scrollView addSubview:myTextView];
myTextView.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:myTextView cache:YES];
myTextView.alpha = 1.0;
[UIView commitAnimations];
You can change several properties (e.g.: frame, transform) to make animations.
http://www.4shared.com/dir/-PGqZEXR/zoominzoomout.html just add this file with your project and call it through this functions
here im2 is UIVIEW and i paste image on it then passing this through view didload like this
- (void)viewDidLoad {
[self loadFlowersInView:im2];
[self.view addSubview:im2];
}
now attach this function like this
- (void) loadFlowersInView: (UIView *) backdrop {
NSString *create_button = nil; // Add the flowers to random points on the screen
for (int i = 0; i < 1; i++) {
MagPicAppDelegate *appdelegate = (MagPicAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *whichFlower = appdelegate.imageName;
UIImage *stemp = [UIImage imageNamed:#"approve.png"];
DragView *dragger = [[DragView alloc] initWithImage:stemp];
dragger.userInteractionEnabled = YES;
dragger.center = CGPointMake(160.0f, 140.0f);
[im2 addSubview:dragger];
[dragger release];
}
[self.view addSubview:im2];
[im2 release];
}

iphone - moving a UIImageView

I'm having difficulty moving an image to another location after the first animation is finished.
the image animates at a point that I've specified, then stops (that works fine). I would then like to move the image to another location and repeat.
here's my code:
-(void) loadTap {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tap1.png"],
[UIImage imageNamed:#"tap2.png"],
[UIImage imageNamed:#"tap3.png"],
[UIImage imageNamed:#"tap4.png"],
nil];
tapImage.animationImages = imageArray;
tapImage.animationRepeatCount = 1;
[imageArray release];
tapImage.animationDuration = 1;
tapImage.animationRepeatCount = 20;
[tapImage startAnimating];
tapImage.center = CGPointMake(156, 110);
}
thanks for any help.
In order to move an image, you should enclose the code to move in an animation block:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
You can also give a method to execute upon completion of the animation with UIView setAnimationDidStopSelector: method.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];
//other stuff
-(void)animateImages{
[tapImage startAnimating];
}