I am drawing a NSString using drawRect and is it possible for me to animate this NSString once it has been drawn? I want it to fade in/out after 3.0 seconds. How can I do so?
Make a UILabel, then use animation to perform your desires
- (void)faceInLabel{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
label.text = #"My String";
[myView addSubview:label];
label.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
label.alpha = 1;
}
completion:^(BOOL finished)
{
[NSTimer timerWithTimeInterval:3 target:self selector:#selector(fadeOutLabel) userInfo:nil repeats:NO];
}];
}
- (void)fadeOutLabel
{
[UIView animateWithDuration:0.5 animations:^{
label.alpha = 0;
}];
}
You cannot directly fade the drawn string itself. You can fade layers and you can fade views. Draw the string onto one of these and fade the layer or view. This is generally most easily done with a UILabel or CATextLayer, but you can draw onto your own layer and fade that if you prefer.
Related
How can I implement marquee effect animation in UIImage.
I have a image of cloud (320*480). I need to animation it continuously left from right without any lagging and disappearing. Below is a sample of html.
http://www.quackit.com/html/codes/html_marquee_code.cfm
2nd from top(Continuous scrolling text:).
Please help
UIImage *clouds = [UIImage imageNamed:#"Clouds"];
UIImageView *cloudView = [[UIImageView alloc] initWithImage:clouds];
CGRect origin = CGRectMake(320, 0, clouds.size.width, clouds.size.height);
CGRect destination = CGRectMake(0, 0, clouds.size.width, clouds.size.height);
cloudView.frame = origin;
[self.view addSubview:cloudView];
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
cloudView.frame = destination;
} completion:nil];
Do this:
-(void)marqueeEffect
{
imgView.frame = CGRectMake(self.view.frame.origin.y,50,320,480); //right frame
//increase time duration as per requirement
[UIView animateWithDuration:3.0
delay: 0.0
options: UIViewAnimationCurveEaseOut
animations:^{
imgView.frame = CGRectMake(-imgview.frame.size.width,50,320,480); //left frame
}
completion:^(BOOL finished){
[self performSelector:#selector(marqueeEffect)];
}];
}
TRY THIS EXAMPLE , ITS FOR UILABEL:
int x;
#synthesize label;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
label=[[UILabel alloc]initWithFrame:CGRectMake(300, 400, 150, 30)];
label.text=#"WWW.SONGS.PK";
label.backgroundColor=[UIColor clearColor];
x=300;
[NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(clickme) userInfo:nil repeats:YES];
}
-(void)clickme
{
x--;
label.frame=CGRectMake(x, 400, 150, 30);
if( x==-150)
{
x=300;
}
[self.view addSubview:label];
}
I am using this for scrolling the uilabel text and this for glow effect. However i want both glow and marquee uilabel text, i have replaced UILabel with RSSGlowLabelclass from above code. but i am not getting the glow effect. can any body tell me how can i achieve that.
It's probably much easier if you code it yourself. Start with this:
float alph = 0.7;
- (void)viewDidLoad {
[super viewDidLoad];
glowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
NSString *string = #"some text";
glowLabel.text = string;
glowLabel.textColor = [UIColor blueColor];
[self.view addSubview:glowLabel];
glowLabel.alpha = alph;
[NSTimer scheduledTimerWithTimeInterval:0.4
target:self
selector:#selector(glowMarquee)
userInfo:nil
repeats:YES];
}
-(void)glowMarquee {
alph = (alph == 1) ? 0.7 : 1; // Switch value of alph
[UIView beginAnimations:#"alpha" context:NULL];
[UIView setAnimationDuration:0.4];
glowLabel.alpha = alph;
[UIView commitAnimations];
}
Now just add the marquee logic to the glowMarquee method, or create a separate method for the marquee and another timer to control it, so the two can be controlled independently.
Everybody..
How to set animation on button click ?
I have two buttons, both have different frame..
First CGRectMake (10 , 10 , 100 , 80 ); It has Black Image,
Second CGRectMake (200, 10 , 210 , 80 ); It has White Image,
When I click on First button of those, It should be animate to move from one frame to another frame..
Only seen like first's button Black Image move to second button location.. Like Image changes of Buttons..
After clicked button frame does not change... Should be remain as First and Second..
I have tried but not got the exact
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
buttonFirst.transform = CGAffineTransformMakeTranslation( buttonSecond.frame.origin.x - buttonFirst.frame.origin.x , buttonSecond.frame.origin.y - buttonFirst.frame.origin.y);
How to animate this ?
Editting :
I need to move one checkers from one CGRect to Another CGRect.. So, It want to display like moving checkers, But i dont want to change CGRect, Only button images looks like that...
Please tell me..
Thanks...
very simple logic
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.20f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[firstButton setFrame:CGRectMake(5, 50, 30, 30)];//moves to second button place
[secondButton setFrame:CGRectMake(5, 5, 30, 30)];//moves to first button place
[UIView commitAnimations];
use the logic accordingly to your use..
well..
you can try this link
[UIView beginAnimation/endAnimation] bock
CGRect oldRectFirst = buttonFirst.frame;
CGRect oldRectSecond = buttonSecond.frame;
[UIView animateWithDuration:0.2f animations:^{
buttonFirst.frame = oldRectSecond;
buttonSecond.frame = oldRectFirst;
}];
Here is example from working program:
[self.buttonFirst setUserInteractionEnabled:NO];
[UIView transitionWithView:self.buttonFirst
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveLinear
animations:^{
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
}
completion:^(BOOL finished){
[self.buttonFirst setUserInteractionEnabled:YES];
}];
Finally i got the answer...
I have set two button in two separate view. after set one timer, and move first button image to another button's position..
Here is a sample code for that..
Step 1 : Set view with Timer
UIView *oldView = [[UIView alloc] init];
oldView = [viewArray objectAtIndex:selectedIndex];
UIView *newView = [[UIView alloc] init];
newView = [viewArray objectAtIndex:[sender tag]];
oldRect = [oldView frame];
newRect = [newView frame];
movingTimer = [[NSTimer alloc] init];
movingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movingCheckers:) userInfo:nil repeats:YES];
imageViewMove = [[UIImageView alloc] init];
[imageViewMove setFrame:oldRect];
[imageViewMove setImage:[UIImage imageNamed:blackManName]];
[self.view bringSubviewToFront:imageViewMove];
[self.view addSubview:imageViewMove];
Step 2 : Moving Image to second position
CGPoint aPoint = CGPointMake(newRect.origin.x - oldRect.origin.x, oldRect.origin.y - newRect.origin.y);
imageViewMove.frame = CGRectMake(imageViewMove.frame.origin.x + (aPoint.x / 6) , imageViewMove.frame.origin.y - (aPoint.y / 6), imageViewMove.frame.size.width, imageViewMove.frame.size.height);
if (imageViewMove.frame.origin.x >= newRect.origin.x)
{
[selectButton setBackgroundImage:nil forState:UIControlStateNormal];
[moveButton setBackgroundImage:imageViewMove.image forState:UIControlStateNormal];
[imageViewMove removeFromSuperview];
[movingTimer invalidate];
}
Now its work.....
I am trying to find the current position of an iPhone ImageView that is animating across the screen.
I create and animate the imageView like so
-(IBAction)button:(UIButton *)sender{
UIImageView *myImageView =[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"ball.png"]];
myImageView.frame = CGRectMake(0, self.view.frame.size.height/2, 40, 40);
[self.view addSubview:myImageView];
[myArray addObject:myImageView];
[UIView animateWithDuration:.5
delay:0
options:(UIViewAnimationOptionAllowUserInteraction) // | something here?)
animations:^{
myImageView.frame = CGRectOffset(myImageView.frame, 500, 0);
}
completion:^(BOOL finished){
[myArray removeObject:myImageView];
[myImageView removeFromSuperview];
[myImageView release];
}
];
}
then to detect the current CGPoint of the imageView I call this method every 60th of a second
-(void)findPoint{
for (UIImageView *projectile in bullets) {
label.text = [NSString stringWithFormat:#"%f", projectile.center.x];
label2.text = [NSString stringWithFormat:#"%f", projectile.center.y];
}
}
However this only gives me the point where the animation ends, I want to get the current CGPoint of the image that is animating across the screen. Is there an option that I need to apply other than UIViewAnimationOptionAllowUserInteraction to do this? If not, how do I get the current position of an animating imageView?
You can use the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed". Just use it like this:
CGRect projectileFrame = [[projectile.layer presentationLayer] frame];
Swift 5:
Get temporary position and size (CGRect) during the animation with:
let currentFrame = projectileFrame.layer.presentation()!.frame
I want to add some text which is a long text lets say a story and I want it to be presented in a moving up fashion. That is it will be displayed in a box and the text will be moved line by line to show the whole story. So one line gets out of the box from top and other comes from the bottom. Like a HTML marquee behavior.
You should look into whether or not UIScrollView's contentOffset property is animatable using UIView animation blocks.
If it's not, then you could try using an NSTimer and in its function, either set the contentOffset or call [scrollView scrollRectToVisible:desiredContentFrame animated:YES]; where desiredContentFrame.origin.y is just a little bit lower than what you previously had it at (depending on the scroll speed you want). Then play around with the firing frequency of your timer and the desiredContentFrame.origin.y's delta to see what looks best and has the best performance.
Here's a quick hack showing both ways:
- (void)startAnimTimer
{
currentRect = CGRectMake(0, 0, 1, 1);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(doScroll) userInfo:nil repeats:YES];
}
- (void)startAnimUIView
{
[UIView beginAnimations:#"scrollUIViewAnim" context:nil];
[UIView setAnimationDuration:20.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint newContentOffset = CGPointMake(0, scrollView.contentSize.height);
scrollView.contentOffset = newContentOffset;
[UIView commitAnimations];
}
- (void)loadView {
[super loadView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
scrollView.contentSize = CGSizeMake(200, 1000);
scrollView.backgroundColor = [UIColor magentaColor];
CGFloat y = 10;
CGFloat yDelta = 21;
for(int i = 0; i < 47; i++)
{
// Add some UILabels to the scrollview just so we can see it animating
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 180, 20)];
label.text = [NSString stringWithFormat:#"Cheezburger %d", i];
[scrollView addSubview:label];
[label release];
y += yDelta;
}
[self.view addSubview:scrollView];
// Comment out one of these
//[self startAnimTimer];
[self startAnimUIView];
}
- (void)doScroll
{
CGFloat scrollDelta = 1.0f;
CGRect newRect = CGRectMake(0, currentRect.origin.y + scrollDelta, currentRect.size.width, currentRect.size.height);
// Scroll the scrollview to the next position
[scrollView scrollRectToVisible:newRect animated:YES];
currentRect = newRect;
}
This method may help (or not):
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
If not, just make a big UILabel and animate its frame using a CABasicAnimation.
I think you want to look at the Scroll View (UIScrollView) in the iPhone SDK.
http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
Depending on your implementation, you may not be able to use it "out of the box" but it should provide the functionality you seem to be describing. I'm not sure it will do the auto-scroll thing but that should be pretty easy to implement... maybe.... But, I'm still confident it could be done.