I want to display remaining time from my NSTimer in NavigationItem title, that's why I can't create outlet for it.
My code is from this question NSTimer problem:
#interface MyController : UIViewController
{
UILabel * theLabel;
#private
NSTimer * countdownTimer;
NSUInteger remainingTicks;
}
#property (nonatomic, retain) IBOutlet UILabel * theLabel;
-(IBAction)doCountdown: (id)sender;
-(void)handleTimerTick;
-(void)updateLabel;
#end
#implementation MyController
#synthesize theLabel;
-(IBAction)doCountdown: (id)sender
{
if (countdownTimer)
return;
remainingTicks = 60;
[self updateLabel];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: #selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void)updateLabel
{
theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}
#end
So, how can I put theLabel.text to label in NavigationBar?
I call method doCountdown in viewDidLoad:
[self doCountdown:nil];
Where is my fault? If I try to watch theLabel, NSLog says (null).
UPD:
And another stupid question. If I try to move label with NSTimer to the right, like here:
[self.navigationItem setTitle:#"Экзамен"];
self.theLabel = [[UILabel alloc] init];
[self doCountdown:nil];
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(260,6,55,32)];
yourLabel.text = self.theLabel.text;
[self.navigationController.navigationBar addSubview:yourLabel];
my timer doesn't start, it shows initial time at label and nothing more.
The label is null because you don't create it anywhere.
In your viewDidLoad you should create it and add to navigation item's title view, e.g. like this:
self.theLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)] autorelease];
self.navigationItem.titleView = self.theLabel;
Don't forget to release theLabel in dealloc.
Related
I am new in iOS and I am facing problem regarding to get the value from custom table view cell to view controller. I am using rate view for rating and I am checking if value of rate is less then 3 then it show have to enter text in the text view and I want to get value in view controller
My code is like this
CustomTableviewcell.h
#interface NextTableview : UITableViewCell<RateViewDelegate,UITextViewDelegate>
{
NSString *StatusValue;
UILabel *lbl;
}
#property(nonatomic,strong) IBOutlet UILabel *staticlbl;
#property(nonatomic,strong) IBOutlet UITextView *commenttxtview;
#property(nonatomic,strong) IBOutlet UILabel *Kpiidlbl;
#property (weak, nonatomic) IBOutlet RateView *rateView;
#property (weak, nonatomic) IBOutlet UILabel *statusLabel;
#end
CustomTableviewcell.m
#synthesize rateView,staticlbl,statusLabel,commenttxtview,Kpiidlbl;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
commenttxtview.layer.borderWidth = 0.70f;
commenttxtview.layer.borderColor = [[UIColor blackColor] CGColor];
commenttxtview.delegate=self;
UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonClickedDismissKeyboard)],
nil];
[doneToolbar sizeToFit];
commenttxtview.inputAccessoryView = doneToolbar;
lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];
[lbl setText:#"Enter Text"];
[lbl setFont:[UIFont systemFontOfSize:12]];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
commenttxtview.delegate = self;
[commenttxtview addSubview:lbl];
statusLabel.hidden=YES;
commenttxtview.hidden=YES;
// Do any additional setup after loading the view from its nib.
self.rateView.notSelectedImage = [UIImage imageNamed:#"not_selected_star#2x.png"];
self.rateView.halfSelectedImage = [UIImage imageNamed:#"half_selected_star#2x.png"];
self.rateView.fullSelectedImage = [UIImage imageNamed:#"selected_star#2x.png"];
self.rateView.rating = 0;
self.rateView.editable = YES;
self.rateView.maxRating = 5;
self.rateView.delegate = self;
Kpiidlbl.hidden=YES;
}
-(void)doneButtonClickedDismissKeyboard
{
[commenttxtview resignFirstResponder];
// commenttxtview.hidden=YES;
}
- (void)textViewDidEndEditing:(UITextView *)theTextView
{
if (![commenttxtview hasText]) {
lbl.hidden = NO;
}
}
- (void) textViewDidChange:(UITextView *)textView
{
if(![commenttxtview hasText]) {
lbl.hidden = NO;
}
else{
lbl.hidden = YES;
}
}
- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating {
self.statusLabel.text = [NSString stringWithFormat:#"%d", rating];
NSLog(#"Rating value =%#",self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(#"Status Value String =%#",StatusValue);
// Hear I am getting value of rating..in StatusValue..
int status=[StatusValue intValue];
if(status<=3)
{
commenttxtview.hidden=NO;
}
else{
commenttxtview.hidden=YES;
}
}
How can I get label and textview value in viewcontroller and I want to set rate view value to rate view after reload table.
Hear in the Image i am getting five star and If I click on less then 3 star it should have to write comment.I am taking rate value in the label.How can I get both label and textvalue in view controller.Please tell me to update question if you want more data.Thanks in Advance!
So write this in your CustomTableViewCell.h
#property(nonatomic,assign)NSInteger selectedRating;
everytime when the rating is updated in the Cell, you have to change the value of this variable.
In the viewController it depends on when you want to get the value of this cell. For example in this method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *yourCell = (CustomerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"%lu",yourCell.selectedRating);
}
Let me know if this was helpful!
Another way without a variable is to read the NSString in the statusLabel and change the string to a NSInteger variable...
If you are using your delegate...
Then implement the delegate RateViewDelegatein the viewController. The delegate is called in the class to which it is assigned...
Try this
You have to add the #property (nonatomic, weak) NSObject<RateViewDelegate>* delegate;
to the header file of your CustomTableViewCell.h in the method
-(UITableViewCell) cellForRowAtIndexPath....
you have to assign the delegate like this
cell.delegate = self;
Now modify your cell delegate method to this
- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating {
self.statusLabel.text = [NSString stringWithFormat:#"%d", rating];
NSLog(#"Rating value =%#",self.statusLabel.text);
StatusValue=statusLabel.text;
NSLog(#"Status Value String =%#",StatusValue);
// Hear I am getting value of rating..in StatusValue..
int status=[StatusValue intValue];
if(status<=3)
{
commenttxtview.hidden=NO;
}
else{
commenttxtview.hidden=YES;
}
if([self.delegate respondsToSelector:#selector(rateView:ratingDidChange:)]){
[self.delegate rateView:rateView ratingDidChange:rating];
}
}
I want this progress view to basically "reset" when its done.
Tried to reset the count to 0 and it does reset but for each reset the timer just becomes faster and faster.
.h
#property (nonatomic, strong) NSTimer *myTimer;
#property (weak, nonatomic) IBOutlet UILabel *progressLabel;
.m
- (void)updateUI:(NSTimer *)timer
{
static int count = 0; count++;
if (count <=100)
{
self.progressView.progress = (float)count/100.0f;
}
else
{
self.myTimer = nil;
[self.myTimer invalidate];
// shoot method for next question
count = 0;
self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progressView.center = self.view.center;
[self.view addSubview:self.progressView];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(updateUI:) userInfo:nil repeats:true];
}
}
You need to invalidate the timer before you set it to nil. As of now, your call to invalidate is a no-op. This means you keep adding more and more active timers.
I'm trying to build a timer app. When the timer hits a certain number (say 5), I want an image to appear. I have the timer working, and the image shows up, but when I declare an if statement to hide the image when the timer has not reached 5, the image shows up anyways. Furthermore, when I declare the UIImageview at the bottom, Xcode reads me the error of "Local declaration of 'images' hides instance variable.
Thank you for any help you can offer! I just started programming.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
-(IBAction)Start:(id)sender; {
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target: self selector:#selector(TimerCount) userInfo: nil repeats:YES];
}
-(void)TimerCount{
CountNumber = CountNumber + 1;
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
if(CountNumber < 6) {
imagesx.hidden = YES;
}
if(CountNumber > 6){
imagesx.hidden = NO;
}
}
-(IBAction)UIImage:(id) sender{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Images.jpg"]];
if (CountNumber < 6){
imagesx.hidden = YES;
}
}
-(IBAction)Stop:(id)sender{
[Timer invalidate];
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
}
-(IBAction)Reset:(id)sender{
CountNumber = 0;
[Timer invalidate];
CountNumber = 0;
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimerCount) userInfo:nil repeats:YES];
[Timer invalidate];
}
-(IBAction)Restart:(id)sender{
CountNumber = 0;
TimerDisplay.text = [NSString stringWithFormat:#"%i", CountNumber];
CountNumber = 0;
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimerCount) userInfo:nil repeats: YES];
if (CountNumber < 6){
imagesx.hidden = YES;
}
if (CountNumber > 6){
imagesx.hidden = NO;
}
}
- (void)viewDidLoad{
//create UIImage view programmatically
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
imagesx.image=[UIImage imageNamed:#"images.jpeg"];
[self.view addSubview:imagesx];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
View Controller.h:
#import <UIKit/UIKit.h>
int CountNumber;
#interface ViewController : UIViewController
{
IBOutlet UILabel *TimerDisplay;
UIImageView *imagesx;
NSTimer *Timer;
}
-(void)TimerCount;
-(IBAction)Start:(id)sender;
-(IBAction)Stop:(id)sender;
-(IBAction)Restart:(id)sender;
#property (strong, nonatomic) IBOutlet UILabel *FirstiPhoneapp;
#property (strong, nonatomic) IBOutlet UILabel *ByJuliaTu;
#property (strong, nonatomic) IBOutlet UIButton *Start;
#property (strong, nonatomic) IBOutlet UIButton *Reset;
#property (strong, nonatomic) IBOutlet UIButton *Terminate;
#property (strong, nonatomic) IBOutlet UIButton *Restart;
#end
You declared your image view as an instance variable with the name "imagesx"
#interface ViewController : UIViewController
{
IBOutlet UILabel *TimerDisplay;
UIImageView *imagesx; <-----your ivar
NSTimer *Timer;
}
So now your view controller has a spot in memory to place an image view.
when viewDidLoad: is called you are doing this:
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
This variable (pointer to a UIImageView) has the same name is the instance variable. This will hide access to that ivar and create a new spot in memory for this local version. In other words when you do this in viewDidLoad:
- (void)viewDidLoad{
//create UIImage view programmatically
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You are declaring a new image view here with the same name so it "overrides" or hides the ivar by creating a new local one:
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
// You are assigning your newly created image view to this local variable
imagesx.image=[UIImage imageNamed:#"Matthijs"];
// adding it to the view, which works but:
[self.view addSubview:imagesx];
// the method returns, this local variable is gone and the ivar is still nil
}
// The ivar is never getting a pointer to your newly created image view. You can't access it after the method returns.
if you put a breakpoint in the timerCount method
Your image view was never set to the ivar (it is nil) so you can't get to it in order to set it to hidden.
The fix is simple. Don't redeclare a new image view in viewDidLoad:, just assign it to your ivar:
imagesx = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200,200)];
Or if you do, declare a new one and change the name:
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 200.0f)];
then assign that to your ivar
imagesx = imageView;
Your code is very hard to follow. You shouldn't create ivars anymore, you can just create a proper for the NSTimer and the UIImageView. Also I'm not sure why you created outlets to the buttons if you're just using the IBActions.
In your viewDidLoad you are redeclaring imagesx and adding it as a subview so when you use the ivar in the timer call, you're actually looking at a nil pointer.
You should declare your NSTimer and the UImageView as properties:
#property (strong, nonatomic) NSTimer * timer;
#property (weak, nonatomic) IBOutlet UIImageView * imageView; // Connect this to an ImageView in the storyboard instead of creating it in code
In your .m file you should access the timer and the image as properties:
self.timer =
self.imageView.image = [UIImage imageNamed:#"..."];
I've been experiencing some difficulties with particle effects. Originally I posted a question about how the particle effects in my app were different when on an ios6 device.
Particle system looks different on iOS 6 and ios7
I Originally designed the app on an ios7 device and got the particles looking like I wanted, but on the ios6 device the particles were smaller and displaced.
I was told it was problem between ios6 and ios7 and that changing the media timing could fix it.
Ive tried everything, but nothing has worked.
Eventually I tried just creating a separate app and pasted in the code from this tutorial - http://weblog.invasivecode.com/post/45058779586/caemitterlayer-and-the-ios-particle-system-lets in the view did load. The particles work as they should, but when I put the same code into my current apps viewDidLoad. The particles were all over the place and not doing what they should be.
I'm wandering if there is something in my current app that Im missing, that is upsetting the particles. Can anyone suggest things to look for?
Originally the particle effects were subclassed and being called after adding a subview to my main view.
Here is my view controller .h code (I've taken out some irrelevant control object properties and outlets)
#interface HHViewController : UIViewController < HHScoreViewdelegate>
{
IBOutlet UIButton* btnStart;
IBOutlet UIButton* btnStop;
IBOutlet UIButton* btnMore;
IBOutlet UIButton* btnHelp;
IBOutlet UIButton* btnTessilaCoil;
IBOutlet UIImageView* imgAnimatedTessila;
IBOutlet UIImageView* imgBubble;
IBOutlet UIImageView* imgSteam;
IBOutlet UIImageView* imgMachineLightsTop;
IBOutlet UIImageView* imgBackground;
NSTimer* StopSignTimer;
NSTimer* timer;
NSTimer* lightMachineTimer;
NSTimer* ComputerLightsTimer;
CGFloat AnimationDelay;
AVAudioPlayer* audioPlayer;
AVAudioPlayer* audioPlayer2; //Used for game end audio, so that it doesnt interupt last letter/number audio
int pageValue;
IBOutlet UIButton* btnSetting;
IBOutlet UIButton* btnIAP;
IBOutlet UIButton* btnNext;
IBOutlet UIButton* btnForward;
IBOutlet UIImageView* imgLightMachine;
IBOutlet UIImageView* imgComputerLights;
NSString* strletterName;
HHScoreView * scoreSelection;
More * MorePanelView;
InAppPurchase * InAppPurchaseView;
NSMutableArray* ButtonBackgroundImages;
NSMutableArray* AlphabetBubbles;
NSMutableArray* SavedBubblePositions;
NSTimer* bubbleBlinkingTimer;
int wrongbubbleTapCount;
IBOutlet UIImageView* imgAlphabetDisplay;
IBOutlet UILabel* lblNextOneToPop;
// Code from Particle Tutorial
CAEmitterLayer *_myEmitter;
CAEmitterCell *_myCell;
// End code form Particle Tutorial
}
// Code from Particle Tutorial
#property(nonatomic, retain) CAEmitterLayer *_myEmitter;
#property(nonatomic, retain) CAEmitterCell *_myCell;
// End code form Particle Tutorial
#property(nonatomic,strong) UILabel* lblNextOneToPop;
#property(nonatomic,strong) NSTimer* SteamTimer;
#property(nonatomic,strong) NSTimer* StopSignTimer;
#property(nonatomic,strong) NSTimer* timer;
#property(nonatomic,strong) NSTimer* lightMachineTimer;
#property(nonatomic,strong) NSTimer* ComputerLightsTimer;
#property(nonatomic,retain)IBOutlet UIButton* btnStart;
#property(nonatomic,retain)IBOutlet UIButton* btnStop;
#property(nonatomic,retain)IBOutlet UIButton* btnMore;
#property(nonatomic,retain)IBOutlet UIButton* btnHelp;
#property(nonatomic,retain)IBOutlet UIButton* btnSetting;
#property(nonatomic,retain)IBOutlet UIButton* btnTessilaCoil;
#property(nonatomic,retain)IBOutlet UIImageView* imgAnimatedTessila;
#property(nonatomic,retain)IBOutlet UIImageView* imgSteam;
#property(nonatomic,retain)IBOutlet UIImageView* imgMachineLightsTop;
#property(nonatomic,retain)IBOutlet UIImageView* imgBackground;
#property(nonatomic,retain)IBOutlet UIScrollView* scScrollView;
#property(nonatomic,retain)IBOutlet IBOutlet UIButton* btnNext;
#property(nonatomic,retain)IBOutlet IBOutlet UIButton* btnForward;
#property(nonatomic,retain)IBOutlet UIImageView* imgLightMachine;
#property(nonatomic,retain)IBOutlet UIImageView* imgComputerLights;
#property(nonatomic,retain)NSString* strletterName;
#property(assign)int allLettersEventCount;
#property(nonatomic,retain) NSArray* ButtonBackgroundImages;
#property Boolean StopButtonClicked;
#property Boolean BubbleAOnscreen;
#property (nonatomic,assign) BOOL cancelAll;
#property int pageValue;
#property int positionRotation;
-(IBAction)tapPiece:(UITapGestureRecognizer *)recognizer;
-(IBAction)actionStartSign:(id)sender;
-(IBAction)actionStopSign:(id)sender;
-(IBAction)actionMoreSign:(id)sender;
-(IBAction)actionHelpSign:(id)sender;
-(IBAction)actionTessilaCoil:(id)sender;
-(IBAction)actionbtnHelp:(id)sender;
-(IBAction)actionbtnSetting:(id)sender;
-(IBAction)actionbtnIAP:(id)sender;
+(HHViewController*)sharedManager;
- (IBAction)purchaseItem:(id)sender;
#property (strong, nonatomic) InAppPurchase *purchaseController;
#end
Here is the code from my viewcontroller .m where I added the code from the tutorial.
#interface HHViewController ()
{
}
#end
#implementation HHViewController
static int OrderingSequence = 0;
#synthesize _myEmitter, _myCell;
/*
TeslarGlowEffect* ShowGlow;
SteamEffect* ShowSteam;
BubbleBurst* PopBubble;
*/
//TeslarTimerSparks* TeslarTimer;
- (void)viewDidLoad
{
[super viewDidLoad];
CFTimeInterval currentTime = [self.view.layer convertTime:CACurrentMediaTime() fromLayer:nil];
NSLog(#"Current media Timing = %f", currentTime);
// Code from tutorial
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer]; // 1
emitterLayer.emitterPosition = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.origin.y); // 2
emitterLayer.emitterZPosition = 10; // 3
emitterLayer.emitterSize = CGSizeMake(self.view.bounds.size.width, 0); // 4
emitterLayer.emitterShape = kCAEmitterLayerSphere; // 5
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell]; // 6
emitterCell.scale = 0.1; // 7
emitterCell.scaleRange = 0.2; // 8
emitterCell.emissionRange = (CGFloat)M_PI_2; // 9
emitterCell.lifetime = 5.0; // 10
emitterCell.birthRate = 10; // 11
emitterCell.velocity = 200; // 12
emitterCell.velocityRange = 50; // 13
emitterCell.yAcceleration = 250; // 14
emitterCell.contents = (id)[[UIImage imageNamed:#"Steam1.png"] CGImage]; // 15
emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell]; // 16
[self.view.layer addSublayer:emitterLayer]; // 17
//end code from tutorial
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[btnStart addTarget:self action:#selector(actionStartSign:) forControlEvents:UIControlEventTouchUpInside];
//NSLog(#"OrderingSequence = %d",OrderingSequence );
imgAnimatedTessila.image = [UIImage imageNamed:KTESSILAIMAGE];
self.alphebetindex = 0;
wrongbubbleTapCount = 0;
//// Load a bunch of arrays used in the game.
[btnHelp addTarget:self action:#selector(actionbtnHelp:) forControlEvents:UIControlEventTouchUpInside];
[btnSetting addTarget:self action:#selector(actionbtnSetting:) forControlEvents:UIControlEventTouchUpInside];
NSString* strFirstTimeloaded = [[NSUserDefaults standardUserDefaults]valueForKey:#"FirstTime"];
if(strFirstTimeloaded == nil)
{
// Show preference page if first time used.
[[NSUserDefaults standardUserDefaults]setValue:#"English" forKey:#"Language"];
LanguagesSelectView* selection = [[LanguagesSelectView alloc]initWithFrame:CGRectMake(0, 0, 1024, 748)];
[self.view addSubview:selection];
[[NSUserDefaults standardUserDefaults]setValue:#"FirstTime" forKey:#"FirstTime"];
}
positionRotation = 0;
self.strletterName = #"29";
self.allLettersEventCount = 0;
[[NSUserDefaults standardUserDefaults] setValue:#"One" forKey:#"Mode"];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(enableSetting) name:#"EnableSetting" object:nil];
//[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(LetterTapped:) name:#"LetterTapped" object:nil];
btnStop.hidden = YES;
btnNext.hidden = YES;
btnForward.hidden = YES;
[self addGestureRecognizersToView];
//Create timer for release of steam from bubble machine
SteamTimer = [NSTimer scheduledTimerWithTimeInterval:5.2 target:self selector:#selector(ShowSteamEffect) userInfo:nil repeats:YES];
//Set Font for lblNextOneToPop
[lblNextOneToPop setFont:[UIFont fontWithName:#"SF Slapstick Comic" size:110]];
lblNextOneToPop.hidden = YES;
}
Here is one of my particle effects that was working on my ios7 iPad, but on the ios6 iPad, the effect looked totally different.
It was after long hrs trying to figure out why the ios7 and ios6 looked different that I tried the exercise of putting the tutorial code into a new app and then it into my own app. In my app the particles are totally different, but as they should be in the new app.
With respect to #matt's comments below. I know there must be something going on with my app, but I can't see it. I've looked all over the web and all through my code and I think I'm missing something.
All I want are three particles effects that appear at specific locations regardless of iOS version. One of them on user touch. The effect below is just one of them. I've tried the CAmediaTiming thing, but that didn't work.
SteamEffect.h
#import <UIKit/UIKit.h>
#interface SteamEffect : UIView
#end
The steamEffect.m looks like this.
#import "SteamEffect.h"
#implementation SteamEffect
{
CAEmitterLayer* SteamEmitter;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
SteamEmitter = (CAEmitterLayer*) self.layer;
//SteamEmitter.emitterPosition= CGPointMake(0, 0);
//SteamEmitter.emitterSize = CGSizeMake(10,10);
CAEmitterCell* Steam = [CAEmitterCell emitterCell];
Steam.birthRate = 50.0f;
Steam.spin = .6f;
Steam.lifetime = 1.7f;
Steam.alphaRange = 0.2f;
Steam.alphaSpeed = 0.2f;
Steam.contents = (id)[[UIImage imageNamed:#"Steam1.png"]CGImage];
Steam.velocity = 30;
Steam.velocityRange = 50.0f;
Steam.emissionLongitude = -60.0f;
Steam.emissionRange = M_1_PI;
Steam.scale = .2f;
Steam.scaleSpeed = .5f;
Steam.yAcceleration = -200.0f;
//SteamEmitter.renderMode = kCAEmitterLayerBackToFront;//kCAEmitterLayerAdditive;
//SteamEmitter.emitterShape = kCAEmitterLayerCircle;
SteamEmitter.emitterCells = #[Steam];
SteamEmitter.emitterPosition = CGPointMake(815, 445);
//Check System Version
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[vComp objectAtIndex:0] intValue] == 7) {
// iOS-6 code
SteamEmitter.beginTime = CACurrentMediaTime();
}
}
return self;
}
-(void)didMoveToSuperview
{
//1
[super didMoveToSuperview];
if (self.superview==nil) return;
[self performSelector:#selector(disableEmitterCell) withObject:nil afterDelay:0.5];
}
-(void)disableEmitterCell
{
[SteamEmitter setValue:#0 forKeyPath:#"birthRate"];
}
+ (Class) layerClass
{
//tell UIView to use the CAEmitterLayer root class
return [CAEmitterLayer class];
}
- (void) setEmitterPosition:(CGPoint)pos
{
SteamEmitter.emitterPosition = pos;
}
- (void) toggleOn:(bool)on
{
//SteamEmitter.birthRate = on? 300 : 0;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
I called the above from my viewcontroller .m using the following. There is also a timer in the viewDidLoad above that calls this every 5 sec. Although that is probably not the way to do it. Probably better to add the view and start it emitting every 5 sec rather than adding a new view each time, but thats a different issue.
- (void)ShowSteamEffect
{
//Check System Version because NSAttributedString only works in ios6 and above.
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
//Create view for Steam particle effect.
//CGRect SteamFrame = CGRectMake(790, 380, 100, 100); //ORIGINAL
CGRect SteamFrame = CGRectMake(0, 0, 0, 0); //(815, 455, 100, 100)
//Show Steam effect
ShowSteam = [[SteamEffect alloc]initWithFrame:SteamFrame];
ShowSteam.hidden = NO;
[self.view insertSubview:ShowSteam aboveSubview:imgComputerLights];
}
I'm having a weird issue when presenting a UIDatepicker as a UITextField subclas's inputView.
What's happening is that the picker is being presented but the second column is being cut by something resulting in having month names truncated at the third or fourth character. Here's a screenshot of this problem:
The whole problem is generated at the begining of the app while setting up some styles, specifically UITableView styles. The line of code responsible for this behaviour is this one:
[[UITableView appearance] setBackgroundColor:[self greyPayAppColor]];
greyPayAppColor is just some custom grey color defined elsewhere. In fact thats the exact color showing in the screenshot covering the months names.
Now, I'm really confused since I don't know how a UITableView apperance message can mess up a UIDatePicker. Another thing is that this problem only occurs with a UIDatePicker used as an inputView of a text field, not on a regular one generated by IB.
For reproduction purposes heres my UITexField subclass that I'm using. It generates two views, a UIDatePicker and a UIToolbar setting them as the text field inputView and inputAccessoryView. It also uses delegation to comunicate when the "Done" and "Cancel" buttons were pressed.
The .h file:
#import <UIKit/UIKit.h>
#protocol DatePickerTextFieldDelegate <NSObject>
#optional
- (void)didDismissDatePickerWithDate:(NSDate *)date; // If date == nil => cancel was pressed
#end
#interface DatePickerTextField : UITextField
#property (nonatomic, weak) id<DatePickerTextFieldDelegate> datePickerDelegate;
#property (nonatomic, strong) NSDate *date;
#end
The .m file:
#import "DatePickerTextField.h"
#interface DatePickerTextField ()
#property (strong, nonatomic) UIToolbar *toolBar;
#property (strong, nonatomic) UIDatePicker *datePicker;
#end
#implementation DatePickerTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[self setup];
}
- (void)setup
{
self.datePicker = [self setupDatePicker];
self.toolBar = [self setupToolBar];
self.inputView = self.datePicker;
self.inputAccessoryView = self.toolBar;
}
- (UIDatePicker *)setupDatePicker
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
return datePicker;
}
- (void)setDate:(NSDate *)date
{
_date = date;
if (_date) {
self.text = [self localizedDateString:_date];
[self.datePicker setDate:_date animated:NO];
}
}
- (NSString *)localizedDateString:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
- (UIToolbar *)setupToolBar
{
CGFloat width = [self superview].frame.size.width;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPressed)];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(donePressed)];
toolBar.items = #[cancel,extraSpace,done];
return toolBar;
}
- (void)cancelPressed
{
[self.datePickerDelegate didDismissDatePickerWithDate:nil];
}
- (void)donePressed
{
[self.datePickerDelegate didDismissDatePickerWithDate:self.datePicker.date];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
I never understood why this happened but I managed to get around it by overriding the appearance call in that particular VC given that it didn't contain a tableView, like this:
// override appearence
[[UITableView appearanceWhenContainedIn:[self.superview class], nil] setBackgroundColor:[UIColor clearColor]];
I would still like to understand this relation between UITableView and UIDatepicker. If anyone know please leave a comment!
Thanks.