UPDATED
I want to make running text line, which way is better to make it?
-(void)viewDidLoad
{
CGSize mySize = CGSizeZero;
mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:#"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];
runningText = [[UIScrollView alloc] initWithFrame:CGRectMake(60, -5, 260, 50)];
grind_fm_text = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, mySize.width, 30)];
[grind_fm_text setUserInteractionEnabled:NO];
[grind_fm_text setBackgroundColor:[UIColor clearColor]];
[grind_fm_text setTextColor:[UIColor whiteColor]];
[grind_fm_text setFont:[UIFont fontWithName:#"Verdana" size:16]];
[grind_fm_text setText:kGrindFMRunningText];
[runningText addSubview:grind_fm_text];
[grind_fm_text release];
[self animate];
}
- (void)animate
{
[UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{
grind_fm_text.frame = CGRectMake(-grind_fm_text.frame.size.width, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height);
// Do your animation in one direction until text is gone
} completion:^(BOOL finished){
grind_fm_text.frame = CGRectMake(260, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height);
// Move scroll position back to original position
[self animate]; // Then call animate again to repeat
}];
}
-(void)songChange
{
CGSize mySize = CGSizeZero;
mySize = [result sizeWithFont:[UIFont fontWithName:#"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];
grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30);
grind_fm_text.text = result;;
[self animate];
}
- (void)startStopStream {
[streamer stop];
//[bufferIndicator stopAnimating];
[CATransaction begin];
[self.view.layer removeAllAnimations];
[CATransaction commit];
grind_fm_text.text = kGrindFMRunningText;
CGSize mySize = CGSizeZero;
mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:#"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];
grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30);
[self animate];
}
[CATransaction begin]; [myView.layer removeAllAnimations]; [CATransaction commit]; doesn't work for me. UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState works strangely: first, it doesn't do completion block as i see, or maybe it does, but animation doesn't start again. And If i click button nothing happening, but when i click it second time, animation begins from another direction and slows until freeze.
You should be able to do this more simply with nested UIView animation blocks. In the animation block have it scroll in the one direction, in the completion block have it scroll in the other direction and in that animation's completion block have it call your animate function over again so it repeats.
Something like this:
- (void)animate
{
[UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
// Do your animation in one direction
} completion:^(BOOL finished){
[UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
// Do your animation in the other direction
} completion:^(BOOL finished){
[self animate];
}];
}];
}
Or if you want it to scroll all the way across then do it again, something like:
- (void)animate
{
[UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
// Do your animation in one direction until text is gone
} completion:^(BOOL finished){
// Move scroll position back to original position
[self animate]; // Then call animate again to repeat
}];
}
It sounds like by default, animations use the UIViewAnimationOptionCurveEaseInOut animation option. You want the UIViewAnimationOptionCurveLinear option. I've updated my code above to include that.
As per an answer to this question: Cancel a UIView animation? you should be able to cancel the animation by calling [myView.layer removeAllAnimations]; where myView is the scroll view being animated. Make sure to import <QuartzCore/QuartzCore.h> at the top to access CALayer methods.
Edit: you may need to call it like this to make sure it runs immediately and not after the next runloop iteration:
[CATransaction begin];
[myView.layer removeAllAnimations];
[CATransaction commit];
Or if that still doesn't work, then likely just changing the options parameters in the UIView method calls to UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState should work, with no need to call removeAllAnimations. In fact, try that first.
Related
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"face.png"]];
circleView.frame = CGRectMake(0, 0, 200, 200);
circleView.layer.cornerRadius = 100;
circleView.center = self.view.center;
[self.view addSubview:circleView];
circleView.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1 options:0 animations:^{
circleView.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
As you can see, I made a view called circleView in this animation method. Then, I created a touchesBegan method and I want to run through the animation again when touching the screen. How should I do that?
As Sujay said, create a new method and put below lines inside that method. Call that method from touchBegan method.
circleView.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1 options:0 animations:^{
circleView.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
I did it. I created 2 properties and I made an animation where it gets small, then another animation that makes to make it bigger.
I want to animate UIView simultaneously. But my UIView is not animated. Side over lap But my UITableView is not animated.
[self.view addSubview:sideview];
[sidevieww setHidden:NO];
[UIView animateWithDuration:0.3 animations:^{
[sidevieww setFrame:CGRectMake(0, 0, sidevieww.frame.size.width, sidevieww.frame.size.height)];
[mytable setFrame:CGRectMake(240,mytable.frame.origin.y,mytable.frame.size.width,mytable.frame.size.height)];
[mynav setFrame:CGRectMake(240,mynav.frame.origin.y,mynav.frame.size.width,mynav.frame.size.height)];
[mybutton setFrame:CGRectMake(240, mybutton.frame.origin.y, mybutton.frame.size.width, mybutton.frame.size.height)];
} completion:^(BOOL finished) {
sideview =true;
}];
That is really not the best way to configure frame.
Replace your code by this one:
[self.view addsubview:sideview];
[sidevieww setHidden:NO];
CGRect frameSideVieww = sidevieww.frame;
frameSideVieww.origin.x = frameSideVieww.origin.y = 0;
CGRect frameMyTable = mytable.frame;
frameMyTable.origin.x = 240;
CGRect frameMyNav = mynav.frame;
frameMyNav.origin.x = 240;
CGRect frameMyButton = mybutton.frame;
mybutton.origin.x = 240;
[UIView animateWithDuration:0.3 animations:^{
[sidevieww setFrame:frameSideVieww];
[mytable setFrame:frameMyTable];
[mynav setFrame:frameMyNav];
[mybutton setFrame:frameMyButton];
}completion:^(BOOL finished) {
sideview =true;
}];
Also, check if your class is subclass of UIViewController.
I want to create a custom alertView that works like instagram app (login/signin view when user enters wrong password or email). The alert animation should drop down and pause for the user to read for about 2 seconds, then the alertView goes up again. How can I do this?
Here is what I have now:
- (UIView *) createAlertViewWithViewController:(UIViewController *)viewController andText:(NSString *)alertText
{
alertView = [[UIView alloc] init];
[alertView setBackgroundColor:ALERT_VIEW_COLOR];
[alertView setFrame:ALERT_VIEW_HIDE_FRAME];
UILabel *alertViewLabel = [[UILabel alloc] init];
[alertViewLabel setFrame:CGRectMake(viewController.view.bounds.origin.x, 7, viewController.view.bounds.size.width, ALERT_VIEW_HEIGHT)];
[alertViewLabel setTextAlignment:NSTextAlignmentCenter];
[alertViewLabel setTextColor:[UIColor whiteColor]];
[alertViewLabel setFont:[UIFont systemFontOfSize:13.0f]];
[alertViewLabel setText:alertText];
[alertView addSubview:alertViewLabel];
CGPoint originalCenter = alertView.center;
[UIView animateWithDuration:0.5
animations:^{
CGPoint center = alertView.center;
center.y += ALERT_VIEW_HEIGHT;
alertView.center = center;
}
completion:^(BOOL finished){
[UIView animateWithDuration:3.0
animations:^{
alertView.center = originalCenter;
}
completion:^(BOOL finished){
;
}];
}];
return alertView;
}
My code right now goes down and goes up immediately but what I want is, when it finishes the drop down animation it pauses for around 2 seconds then do the goes up animation.
I have been searching for 3 days now, but I haven't found anything one this.
Example image when it should pause for 2 seconds:
Im guessing you are looking for one of the extended versions of [UIView animateWithDuration:...
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Pass in a delay to wait your animation.
Would you like a this library?
https://github.com/problame/CSNotificationView
Example
[CSNotificationView showInViewController:self
tintColor:[UIColor colorWithRed:0.000 green:0.6 blue:1.000 alpha:1]
image:nil
message:#"What's the meetup called?"
duration:2.f];
Try this:
UILabel *myLable=[[UILabel alloc]init];
myLable.frame=CGRectMake(0, -50, 320, 50);
myLable.backgroundColor=[UIColor grayColor];
myLable.textAlignment=NSTextAlignmentCenter;
myLable.text=#"Your Error Message";
[self.view addSubview:myLable];
[self ShowAnimation:myLable currentFrame:CGRectMake(0, -50, 320, 50) newFrame:CGRectMake(0, 0, 320, 50)];
.
-(void)ShowAnimation:(UILabel *)aLable currentFrame:(CGRect)aCurrentFrame newFrame:(CGRect)aNewFrame
{
[aLable setFrame:aCurrentFrame];
[UIView animateWithDuration:1.5 animations:^{
[aLable setFrame:aNewFrame];
}completion:^(BOOL finished)
{
[aLable setFrame:aNewFrame];
[UIView animateWithDuration:1.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn
animations:^{
[aLable setFrame:aCurrentFrame];
}
completion:^(BOOL finished) {
}];
}];
}
I was wondering if it was possible to apply a flashing animation to a UIButton. I have searched but only found the code for a pulse animation which changes the size of my UIButton continuously. I instead was thinking about some kind of flashing animation to alert the user he has to press the button. The only approach to the problem I can think of is by changing the alpha constantly using:
[self setAlpha:0.5];
...but it won't be as visible as a flashing button.
Perhaps not the best way, and doesn't really allow you to stop the flashing... but this is simple, works, and does not hinder user interaction:
- (void)viewDidLoad
{
[self flashOn:myButton];
}
- (void)flashOff:(UIView *)v
{
[UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
v.alpha = .01; //don't animate alpha to 0, otherwise you won't be able to interact with it
} completion:^(BOOL finished) {
[self flashOn:v];
}];
}
- (void)flashOn:(UIView *)v
{
[UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
v.alpha = 1;
} completion:^(BOOL finished) {
[self flashOff:v];
}];
}
Reading all the answers so far I found following solution. It repeats a number of times and does the job for me.
CGFloat oldAlpha = v.alpha;
CGFloat minAlpha = 0.2;
enum UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut;
[UIView animateWithDuration:.3 delay:0.3 options:options animations:^{
[UIView setAnimationRepeatCount:4];
v.alpha = minAlpha;
}
completion:^(BOOL finished) {
v.alpha = oldAlpha;
}];
Try this one:
call this method when you want blink/flash the button
blinkTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(toggleButtonImage:) userInfo:nil repeats:YES];
and write this method
- (void)toggleButtonImage:(NSTimer*)timer
{
if(toggle)
{
[blinkBtn setImage:[UIImage imageNamed:#"ButtonImage.png"] forState: UIControlStateNormal];
}
else
{
[blinkBtn setImage:[UIImage imageNamed:#"ButtonImage1.png"] forState: UIControlStateNormal];
}
toggle = !toggle;
}
in .h write this one
NSTimer *blinkTimer;
BOOL toggle;
and invalidate the timer where you want to stop the flashing/blinking
[blinkTimer invalidate];
Maybe you could have two different .png files [buttons] that cycle back and forth in a loop, assign the same action to them, and have this kick off whenever a certain condition is met. I'd write the code out but it would likely be full of errors. Have you tried something like that?
I did this by creating my own control, subclassing UIControl since Apple doesn't recommend screwing with the view hierarchy of UIButton. I add a background imageView representing the standard background image of the button, and a "glowing" imageView above the background to represent the lit-up state, and toggle its opacity to make it pulse.
I additionally toggle the layer's shadow opacity to make it glow.
Initializing Code:
- (void)TS_commonButtonInit
{
UIImage *shoutoutBackground = [UIImage imageNamed:#"root-navigation-bar-share-button"];
UIImage *shoutoutHighlightedBackground = [UIImage imageNamed:#"root-navigation-bar-share-button-highlighted"];
UIImage *shoutoutPulseImage = [UIImage imageNamed:#"root-navigation-bar-share-button-glowing"];
shoutoutBackground = [shoutoutBackground stretchableImageWithLeftCapWidth:7 topCapHeight:0];
shoutoutHighlightedBackground = [shoutoutHighlightedBackground stretchableImageWithLeftCapWidth:7 topCapHeight:0];
shoutoutPulseImage = [shoutoutPulseImage stretchableImageWithLeftCapWidth:7 topCapHeight:0];
[[self backgroundView] setImage:shoutoutBackground];
[[self backgroundView] setHighlightedImage:shoutoutHighlightedBackground];
[self setGlowingImage:shoutoutPulseImage];
[self setExclusiveTouch:YES];
[self addSubview:[self backgroundView]];
[self addSubview:[self glowingImageView]];
[[self layer] setShadowColor:[[UIColor colorWithHexString:#"ffc521" alpha:1] CGColor]];
[[self layer] setShadowOpacity:0];
[[self layer] setShadowRadius:5];
[[self layer] setShadowOffset:CGSizeMake(0, 0)];
[[self layer] setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:6] CGPath]];
}
Pulsing Code:
- (void)pulse:(NSInteger)numberOfTimes
{
CGFloat pulseLength = .8;
[[self glowingImageView] setAlpha:0];
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[pulseAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[pulseAnimation setDuration:pulseLength];
[pulseAnimation setRepeatCount:numberOfTimes];
[pulseAnimation setAutoreverses:YES];
[pulseAnimation setFromValue:#(0)];
[pulseAnimation setToValue:#(1)];
[pulseAnimation setRemovedOnCompletion:YES];
[[self layer] setShadowOpacity:0];
CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:#"shadowOpacity"];
[shadowAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[shadowAnimation setDuration:pulseLength];
[shadowAnimation setRepeatCount:numberOfTimes];
[shadowAnimation setAutoreverses:YES];
[shadowAnimation setFromValue:#(0)];
[shadowAnimation setToValue:#(1)];
[shadowAnimation setRemovedOnCompletion:YES];
[[[self glowingImageView] layer] addAnimation:pulseAnimation forKey:#"opacity"];
[[self layer] addAnimation:shadowAnimation forKey:#"shadowOpacity"];
}
In my app, I have a scrollview with "paging enabled" and when I chose a page an imageview start its animation in this way
- (void) beginWinAnimation{
[UIView animateWithDuration:2.5
animations:^{successView.alpha = 1.0;}
completion:^(BOOL finished){
[UIView animateWithDuration:2.5
animations:^{successView.alpha = 0;}];
}];}
During this animation I can see the scrollview under this successView and I can move scrollview page; I want that during animation you can't move the scrollview but only when animation is finished, is it possible?
Yes it's possible. When the animation starts set [scrollView setScrollEnabled:NO]; After your animation is complete set it back to YES.
So in your code, it will look like -
- (void) beginWinAnimation{
[scrollView setScrollEnabled:NO];
[UIView animateWithDuration:2.5
animations:^{
successView.alpha = 1.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.5
animations:^{successView.alpha = 0;}
completion:^(BOOL finished){
[scrollView setScrollEnabled:YES];
}];
}];}
See if this works.