UIView Animation isn't working in SKScene
MyScene.h
#property (nonatomic) SKLabelNode *tutorialLabel;
MyScene.m
- (IBAction)hideLabel:(id)sender {
[UIView animateWithDuration:1 animations:^(void) {
[_tutorialLabel setAlpha:0.0];
}];
}
Is there a better way to hide an SKLabelNode
If you want to fade out the label, you can use SKActions:
- (IBAction)hideLabel:(id)sender {
[_tutorialLabel runAction:[SKAction fadeOutWithDuration:1.0f]];
}
Related
I need to perform a blinking effect on a image once the user click on a particular button. But my code doesn't work:
- (void)next
{
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
NSLog(#"Done"); // <- USED AS COUNTER
[image setAlpha:0];
[image setAlpha:0.5];
} completion:nil];
}
What happens is my image changing its alpha to 0.5 and stops. Furthermore my console shows DONE just one time. What did I wrong?
I just created a test project and this blinked just fine...
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong)UIView *blinker;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.blinker = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.blinker.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.blinker];
[self next];
}
- (void)next
{
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
NSLog(#"Done"); // <- USED AS COUNTER
[self.blinker setAlpha:0];
[self.blinker setAlpha:0.5];
} completion:nil];
}
With the exception that you expect the log to continue to print out in the console. So I suspect you are doing something to your image. You aren't calling self. so I am guessing it is an instance variable (ivar) and you are changing what it points to somewhere else in the code.
If you need a counter to go off every blink you may want to consider something like this...
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong)UIView *blinker;
#property (nonatomic,)NSInteger counter;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.blinker = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.blinker.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.blinker];
[self next];
}
- (void)next
{
[UIView animateWithDuration:1.0 animations:^{
self.counter++;
NSLog(#"Counter: %#", #(self.counter));
self.blinker.alpha = 0.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
self.blinker.alpha = 1.0;
} completion:^(BOOL finished) {
//check to see if you want to continue blinking if so call next again
[self next];
}];
}];
}
I don't have any context as to why you want to count, but hopefully that helps.
The code I used to animate a UITextField worked for me until XCode Version 6.0 (6A215l). Now XCode6 Beta7 not reposition UITextField. What should I do to make it work?
Here the code:
#interface TableConfigViewController ()
#property(nonatomic, strong) IBOutlet UITextField *customTableName;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[self.customTableName setDelegate:self];
}
- (void)animateTextField:(UITextField*)textField
up:(BOOL)up
{
const float movementDuration = 0.5f;
const int movementDistance = 380;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"animation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementDuration];
self.customTableName.frame = CGRectOffset(self.customTableName.frame, 0, movement);
[UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField*)textField
{
[self animateTextField:textField up:NO];
}
Thank you very much in advance
Not sure if this is the source of your problem but you should consider using the block based APIs (available since iOS 4.0) for UIView animations.
From Apple's docs regarding beginAnimations:context:
"Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead."
For example (untested):
- (void)animateTextField:(UITextField*)textField
up:(BOOL)up
{
const float movementDuration = 0.5f;
const int movementDistance = 380;
int movement = (up ? -movementDistance : movementDistance);
[UIView animateWithDuration:movementDuration
animations:
^{
self.customTableName.frame = CGRectOffset(self.customTableName.frame, 0, movement);
}
];
}
I want a button to trigger the following action. When clicked it's supposed to push my logo up and out of the view and remove it. When it's clicked once more it's supposed to do the reverse thing. Meaning the image is created and pushed down back into the view. I'm using storyboard to set up my interface. My issue is that I can't get the animation to trigger. Posting my code below:
In TableViewController.h
#interface TableViewController : UITableViewController{
BOOL status;
}
#property (weak, nonatomic) IBOutlet UIImageView *animateLogo;
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)onClick:(id)sender;
- (void)showHideView;
#end
In TableViewController.m
#synthesize animateLogo, button;
- (void)viewDidLoad{
[super viewDidLoad];
status = YES;
}
- (IBAction)onClick:(id)sender{
[self showHideView];
}
if(status){
[UIView
animateWithDuration:1.5
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
[animateLogo setFrame:CGRectOffset([animateLogo frame], 0, -animateLogo.frame.size.height)];
}
completion:^(BOOL completed) {}
];
status = NO;
[button setTitle:#"Show" forState:UIControlStateNormal];
}
else{
[UIView
animateWithDuration:1.5
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
[animateLogo setFrame:CGRectOffset([animateLogo frame], 0, animateLogo.frame.size.height)];
}
completion:^(BOOL completed) {}
];
status = YES;
[button setTitle:#"Hide" forState:UIControlStateNormal];
}
}
EDIT:
The animation is working if I programmatically code the UIImageView. So the issue is with the storyboard somehow. I prefer to get this working with storyboard so I can get a better overlook on the layout.
UIImage *test = [UIImage imageNamed:#"logo.png"];
animateLogo = [[UIImageView alloc] initWithImage:test];
[self.view addSubview:animateLogo];
[animateLogo setFrame:CGRectOffset([animateLogo frame], 0, animateLogo.frame.size.height)];
Problem sovled. I used a different approach were I animated the UIView instead of the UIImageView. I's now working like a charm.
When I test my app on my device, from Xcode, the iAd works, but when I have uploaded it to the App Store and test it on a friend's iPhone, it's just white!
Here is my code:
.h:
#interface pinpongViewController : UIViewController {
ADBannerView *banner;
}
#property (nonatomic, assign)BOOL bannerIsVisible;
#property (nonatomic, retain)IBOutlet ADBannerView *banner;
#end
.m:
#synthesize banner, bannerIsVisible;
-(void)bannerViewDidLoadAd:(ADBannerView *)abanner {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 50.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)aBanner didFailToReceiveAdWithError:(NSError *)error {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, -320.0);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
#end
Have you enabled the iAd network for your app, before submitting to the app store? It is required to enable iAd network(in iTunes connect) or else only a white screen appears! Hope this helps.
I'm looking for some guidance on the best way to implement stock ticker style right-to-left scrolling of CALayers in Core Animation on OSX. I'm pretty new to Cocoa and don't know the best way to implement this.
I have a continuous stream of news items and stock details that I turn into CALayers (made up of 1 image and a CATextLayer) and I want to animate the CALayers from right to left of my custom view.
The news and stock information is constantly updating so I would like to add 1 item at a time to the view, scroll it right to left until the right-most point of the CALayer is showing, then add another CALayer to the view and start scrolling that as well. I would like to do this dynamic updating instead of taking a big snapshot of all my data, turning it into a big horizontal CALayer and scrolling that.
I'm looking for guidance on how to achieve this sort of effect -
do I manually animate each new CALayer along a path in the view? Or should I be using CAScrollLayer to achieve this effect?
Many thanks
Glen.
I'd do this with Quartz Composer and put a QCView in your app. It's a lot less work than you imagine.
You perhaps don't need to customize anything to do this. Here self is an object of class extending UIScrollView and with a UILabel to display the text.
////
//Header
#import <UIKit/UIKit.h>
#interface TickerScrollView : UIScrollView
- (void)displayText:(NSString *)string;
- (void)clearTicker;
#property (nonatomic, retain, readwrite) UILabel *textLabel;
#end
//////
//Implementation
#implementation TickerScrollView
#synthesize textLabel;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self setFrame: frame];
[self setBounces: NO];
[self setShowsVerticalScrollIndicator:NO];
[self setShowsHorizontalScrollIndicator:NO];
[self setBackgroundColor:[UIColor greenColor]];
[self initialiseTextLabel];
}
return self;
}
- (void)initialiseTextLabel {
textLabel = [[UILabel alloc] initWithFrame:self.bounds];
[textLabel setTextAlignment:UITextAlignmentLeft];
[textLabel setNumberOfLines:1];
[textLabel sizeToFit];
[self addSubview:textLabel];
[self setScrollEnabled:YES];
}
- (void)displayText:(NSString *)string {
[self clearTicker];
[textLabel setText:string];
[textLabel sizeToFit];
[self beginAnimation];
}
- (void)clearTicker {
[textLabel setText:#""];
[textLabel sizeToFit];
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
}
- (void)beginAnimation {
CGFloat text_width = textLabel.frame.size.width;
CGFloat display_width = self.frame.size.width;
if ( text_width > display_width ) {
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
CGPoint terminal_origin = CGPointMake(textLabel.frame.size.width - self.frame.size.width, textLabel.frame.origin.y);
float duration = (text_width - display_width)/40;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:duration];
[self setContentOffset:terminal_origin];
[UIView commitAnimations];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)dealloc {
[textLabel release];
[super dealloc];
}
#end