Particle system looks different on iOS 6 and ios7 - ios

I have a couple of particle systems on my app that I checked originally on an iPad 3 with ios7. The particles looked and behaved like I expected them, but when I test on an iPad2 with ios6. The particles are displaced up and are smaller. I have a subclass for the particles. Here's the code for one of my particle systems. I was wandering if the CGREctMake is positioning the particle in a different area on a non retina display, but that doesn't happen when I position other things on the display.
Code to call the particles.
- (void)ShowSteamEffect
{
//Create view for Steam particle effect.
CGRect SteamFrame = CGRectMake(790, 380, 100, 100);
//Show Steam effect
ShowSteam = [[SteamEffect alloc]initWithFrame:SteamFrame];
ShowSteam.hidden = NO;
[self.view insertSubview:ShowSteam aboveSubview:imgComputerLights];
}
Steam subclass.
#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;
Steam.spin = .6;
Steam.lifetime = 1.7;
Steam.alphaRange = 0.2;
Steam.alphaSpeed = 0.2;
Steam.contents = (id)[[UIImage imageNamed:#"Steam1.png"]CGImage];
Steam.velocity = 30;
Steam.velocityRange = 50;
Steam.emissionLongitude = -60;
Steam.emissionRange = M_1_PI;
Steam.scale = .2;
Steam.scaleSpeed = .5;
Steam.yAcceleration = -200;
SteamEmitter.renderMode = kCAEmitterLayerBackToFront;
SteamEmitter.emitterShape = kCAEmitterLayerCircle;
SteamEmitter.emitterCells = #[Steam];
}
return self;
}
-(void)didMoveToSuperview
{
[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;
}

Related

CGFloat MinimumValue vs CurrentValue

I created a custom slider with a uiview class ... everything works fine but I just have a problem ...
in my view controller implements the Custom Slider class this way
Main ViewController
#pragma mark SMALL STATS
-(KPStatsSlider *)statsSlider {
if (!_statsSlider) {
_statsSlider = [[KPStatsSlider alloc] init];
_statsSlider.minimumValue = 18;
_statsSlider.maximumValue = 30;
_statsSlider.currentValue = 12;
_statsSlider.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.statsSlider];
[self.statsSlider.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:115].active = YES;
[self.statsSlider.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:0].active = YES;
[self.statsSlider.heightAnchor constraintEqualToConstant:70].active = YES;
[self.statsSlider.leftAnchor constraintEqualToAnchor:self.view.centerXAnchor constant:0].active = YES;
}
return _statsSlider;
}
as you can see, I can assign the value:
Current /
Minimum /
Maximum
In my personalized UIView I implemented this feature to prevent the CurrentValue value being smaller than MinimumValue but I can not get it running can you help me with this?
Custom Slider UIView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self defaultValue];
[self setupStatsTitle];
[self trackLine];
if (self.currentValue <= self.minimumValue) {
self.currentValue = self.minimumValue;
}
}
return self;
}
Seems like you want
if (self.currentValue <= self.minimumValue) {
self.currentValue = self.minimumValue;
}
In the setter of currentValue. For example:
- (void)setCurrentValue:(CGFloat)currentValue {
_currentValue = currentValue;
if (_currentValue < self.minimumValue) {
_currentValue = self.minimumValue;
}
if (_currentValue > self.maximumValue) {
_currentValue = self.maximumValue
}
}
Your code currently only checks when the slider is initialized, but not if currentValue is set later.
In general, I prefer to access an instance variable directly if I’m in that instance variable’s setter/getter to avoid confusion and possible infinite recursion, but your code as written is okay.

IOS RoundView make child ImageView to be rounded

I've created this custom UIView named RoundView:
#import <QuartzCore/QuartzCore.h>
#implementation RoundedView
+(UIColor *)grayUIColor {
return [UIColor colorWithRed:161.0/255.0 green:157.0/255.0 blue:164.0/255.0 alpha:1.0];
}
+(UIColor *)darkBlueUIColor {
return [UIColor colorWithRed:86.0/255.0 green:88.0/255.0 blue:87.0/255.0 alpha:1];
}
+(UIColor *)greenUIColor {
return [UIColor colorWithRed:51.0/255.0 green:141.0/255.0 blue:130.0/255.0 alpha:1];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
{
//add pico icon as default image
_defaultImage = [UIImage imageNamed:#"actionBar_pico_icon"];
_isCreated = NO;
}
return self;
}
-(UIView*)createRoundViewWithBorder:(UIColor*)borderColor andPaddingColor:(UIColor*)paddingColor{
_borderColor = borderColor;
_paddingBackgroundColor = paddingColor;
[self setViewAppearance];
[self addImageToView];
_isCreated = YES;
return self;
}
/**
Set the current view appearance
*/
-(void) setViewAppearance {
self.layer.borderWidth = 1.5;
self.layer.borderColor = [_borderColor CGColor];
self.backgroundColor = _paddingBackgroundColor;
}
-(void) addImageToView {
CGRect frame = CGRectMake(0, 0, self.frame.size.width - 5, self.frame.size.height - 5);
_imageView = [[UIImageView alloc] initWithFrame:frame];
//calculate center x
float x = (self.frame.size.width - _imageView.frame.size.width) / 2;
//calculate center y
float y = (self.frame.size.height - _imageView.frame.size.height) / 2;
//create new frame
frame = CGRectMake(x, y, _imageView.frame.size.width, _imageView.frame.size.height);
_imageView.image = _defaultImage;
_imageView.frame = frame;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_imageView];
[self makeViewRounded:_imageView];
[self makeViewRounded:self];
}
-(UIView*) makeViewRounded:(UIView*)view {
//set the look of the image
view.layer.cornerRadius= self.frame.size.height /2;
view.layer.opaque = NO;
view.layer.masksToBounds = YES;
return view;
}
-(void)updateImage:(UIImage *)image {
_image = image;
_imageView.image = image;
}
-(void)reset {
[self updateImage:_defaultImage];
}
#end
an example for the output will be :
If you look closely you will notice that the border is a circle, but the image view has edges.
How can i make the Image smooth as well ?
self.imgViewbg.layer.cornerRadius = self.imgViewbg.frame.size.width / 2;
self.imgViewbg.clipsToBounds = YES;
u Can Try This Code. And Implement in your Project..
It Will Definetly Work.. for u
I think the problem seems to be here:
view.layer.cornerRadius= self.frame.size.height /2;
Try giving it a constant small number and see the change. May be the height/2 is not making a perfect circle. You can give a smaller value than height/2 and see the change. It a wild guess by watching your image.

Is this the right way to start and stop a particle system

I'm currently practicing with particle systems and I was wondering if the following code is the right way to stop and start a particle when a button is tapped?
The code works fine, I touch the start button and the particle starts, I touch the stop button and the particle stops but I'm not sure if removeFromSuperLayer is the right method to use. As I said, the code does what I need but I just want to make sure that the particle won't keep running in the background even after calling removeFromSuperLayer and end-up wasting resources.
- (IBAction)stopAnimation:(id)sender
{
[emitterLayer removeFromSuperlayer];
}
- (IBAction)startAnimation:(id)sender
{
[self particle];
}
-(void) particle
{
emitterLayer = [CAEmitterLayer layer];
emitterLayer.emitterPosition = CGPointMake(50 ,50);
emitterLayer.emitterZPosition = 10;
emitterLayer.emitterSize = CGSizeMake(10,10);
emitterLayer.emitterShape = kCAEmitterLayerSphere;
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.scale = 0.1;
emitterCell.scaleRange = 0.2;
emitterCell.emissionRange = (CGFloat)M_PI_2;
emitterCell.lifetime = 10;
emitterCell.birthRate = 5;
emitterCell.velocity = 20;
emitterCell.velocityRange = 50;
emitterCell.yAcceleration = 0;
emitterCell.contents = (id)[[UIImage imageNamed:#"particleImage.png"] CGImage];
emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];
[self.view.layer addSublayer:emitterLayer];
}
Thanks a lot
You could use a method in which you put the following:
- (void)stopEmitting
{
self.emitterCell.birthRate = 0.0f;
}
With this you should be able to stop the emitting, without having to remove and re-create the layer each time when the start button is pressed.
To start again, simply do:
- (void)startEmitting
{
self.emitterCell.birthRate = <VAlUE HERE (greater than 0)>;
}
Hope this helps.
Rather than change the birthRate of the each of the emitterCells you can change the birthrate of the emitterLayer itself.
- (void)stopEmitting
{
self.emitterLayer.birthRate = 0;
}
- (void)startEmitting
{
self.emitterLayer.birthRate = 1;
}
It's funny but just modifying the birthRate like this self.emitterCell.birthRate = 0.0f; in a method doesn't stop the emitterCell, in fact it looks like if it appends instead of stopping it, in other words if I change it to self.emitterCell.birthRate = 100; it adds 100 more particles to the existing particles. Lucky I found the solution.
I basically had to give my emitterCell a name emitterCell.name = #"_myCell"; and then in my stop method modify it like this [emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:#"emitterCells._myCell.birthRate"]; and it worked.
This is what I did that worked. This is assuming you already have an image in your project named myImage.
#import "SpriteViewController.h"
#implementation SpriteViewController
CAEmitterLayer *emitterLayer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)stopAnimation:(id)sender
{
[emitterLayer setValue:[NSNumber numberWithInteger:0] forKeyPath:#"emitterCells._myCell.birthRate"]; // new code
}
- (IBAction)startAnimation:(id)sender
{
[self particle];
}
-(void) particle
{
emitterLayer = [CAEmitterLayer layer];
emitterLayer.emitterPosition = CGPointMake(50 ,50);
emitterLayer.emitterZPosition = 10;
emitterLayer.emitterSize = CGSizeMake(10,10);
emitterLayer.emitterShape = kCAEmitterLayerSphere;
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.name = #"_myCell";// new code
emitterCell.scale = 0.1;
emitterCell.scaleRange = 0.2;
emitterCell.emissionRange = (CGFloat)M_PI_2;
emitterCell.lifetime = 10;
emitterCell.birthRate = 5;
emitterCell.velocity = 20;
emitterCell.velocityRange = 50;
emitterCell.yAcceleration = 0;
emitterCell.contents = (id)[[UIImage imageNamed:#"myImage.png"] CGImage];
emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];
[self.view.layer addSublayer:emitterLayer];
}
#end

Spritekit drastic frame rate drop

I have tried my best to boil this question down as simple as possible. I have a coin object in my game:
#implementation
-(CollectableCoin*)initWithLocation:(CGPoint) Location andValue: (int) val
{
self = [super initWithImageNamed:#"coin"];
[self setScale:.35];
_value = val;
_collected = false;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = APAColliderTypeCoin;
self.physicsBody.collisionBitMask = APAColliderTypeBall;
self.physicsBody.mass = 0.00009;
self.physicsBody.restitution = .35;
self.position = Location;
self.name = #"collectableCoin";
return self;
}
#end
I also have a shelf object:
#implementation Shelf
-(Shelf*)initWithLocation:(CGPoint) location andWidth:(NSInteger) width
{
self = [super initWithImageNamed:#"shelf"];
if(self)
{
self.size = CGSizeMake(width, HEIGHT);
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.dynamic = false;
self.physicsBody.restitution = 0;
self.position = location;
self.name = #"shelf";
SKSpriteNode* topOfShelf;
if(width > 5)
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width-2, 1)];
else
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width, 1)];
topOfShelf.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:topOfShelf.size];
topOfShelf.physicsBody.restitution = 1;
topOfShelf.physicsBody.dynamic = false;
topOfShelf.position = CGPointMake(0, location.y + self.size.height/2);
NSLog([NSString stringWithFormat:#"%f", location.y + self.size.height/2]);
NSLog([NSString stringWithFormat:#"%f", location.y]);
topOfShelf.name = #"shelf";
[self addChild:topOfShelf];
}
return self;
}
#end
I create a scene like so:
-(id)initWithSizeTest:(CGSize)size
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
_gameState = READYTOSTART;
self.physicsWorld.contactDelegate = self;
if (self = [super initWithSize:size])
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
for(int i = 0; i < 25; i++)
{
CollectableCoin* orb = [[CollectableCoin alloc] initWithLocation:CGPointMake(i*10, self.size.height*.75) andValue:1];
[self addChild:orb];
}
Shelf* shelf = [[Shelf alloc] initWithLocation:CGPointMake(self.size.width/2, self.size.height/2) andWidth:self.size.width];
[self addChild:shelf];
}
return self;
}
Here is the touchesBegan method:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if(_gameState == READYTOSTART)
{
self.physicsWorld.gravity = CGVectorMake(0, -2.0);
_gameState = PLAYING;
[[self childNodeWithName:#"taptostart"] removeFromParent];
}
When the scene starts, I have a row of coins hovering above a shelf, gravity is disabled, and I have a solid 60fps. When I tap the screen, the touchesbegan function enables gravity and the coins fall on the shelf, and the frame rate drops to 5fps. The didBeginContact function is not being called because the shelf object is not dynamic nor does it have contact or collision bitmasks, so I am fairly sure that it is not being overloaded by extraneous calls to didBeginContact. This happens on an iPad mini and an iPhone 4s, but not in any of the simulators. This is a very simple example of the actual problem that I am having. Anyone have any insights?

UIKit Particle Systems in ios7. Number of particles increased

The particle system on ios7 seem to be working different, than on ios6 and ios5. The number of particles increased.
The same issue happens with all particle effects in the app.
The only working solution is to check, if it is ios7 and decrease the particle birth rate. Is there a better solution?
The code of particle emitter view.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//initialize the emitter
_emitter = (CAEmitterLayer*)self.layer;
_emitter.emitterPosition = CGPointMake(self.bounds.size.width /2, self.bounds.size.height/2 );
_emitter.emitterSize = self.bounds.size;
_emitter.emitterMode = kCAEmitterLayerAdditive;
_emitter.emitterShape = kCAEmitterLayerRectangle;
}
return self;
}
- (void)didMoveToSuperview
{
//Check if parent is initialized
[super didMoveToSuperview];
if (self.superview==nil) return;
//Load png
UIImage* texture = self.explosionImage; //[UIImage imageNamed:#"particle.png"];
NSAssert(texture, #"particle.png not found");
//Create a new emitter cell
CAEmitterCell* emitterCell = [CAEmitterCell emitterCell];
//Set the cell’s contents property to the texture you loaded
emitterCell.contents = (__bridge id)[texture CGImage];
//Name the cell “cell”
emitterCell.name = #"cell";
//Parameters
emitterCell.birthRate = self.birthRate;// 40;//1000
emitterCell.lifetime = 2.8;
emitterCell.lifetimeRange = 0.7;
//Set the cell’s color to randomly vary its components
if (!self.explosionColor) {
emitterCell.blueRange = 0.33;
emitterCell.blueSpeed = -0.33;
emitterCell.redRange = 0.33;
emitterCell.redSpeed = -0.33;
emitterCell.greenRange = 0.33;
emitterCell.greenSpeed = -0.33;
}
//Explosion color
if (self.explosionColor) emitterCell.color = [self.explosionColor CGColor];
//velocity
emitterCell.velocity = IS_IPAD?40:20;//160
emitterCell.velocityRange = RANDOMF(10, 20);//15
//Alpha
emitterCell.alphaSpeed = -0.73;
emitterCell.alphaRange = 0.9;
//Scale
emitterCell.scale = IS_IPAD?0.6:0.30;//0.5
emitterCell.scaleRange = 0.6;//0.5
emitterCell.scaleSpeed = 0;
//Range
emitterCell.emissionRange = M_PI*2;//2
//Add the cell to the emitter layer
_emitter.emitterCells = #[emitterCell];
[self performSelector:#selector(disableEmitterCell) withObject:nil afterDelay:self.cellIsEmitting];
[self performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:self.emittingInView];
}
When running on an iOS7, it looks as if the particle system started animating earlier than the time the emitter layer is added.
It is probably a bug, and, hopefully, will be solved in future.
For now, to get a similar behavior as on iOS6, the CAEmitterLayer's beginTime can be set to the current time:
_emitter.beginTime = CACurrentMediaTime();

Resources