我在使用粒子效果方面遇到了一些困难。最初我发布了一个关于在ios6设备上我的应用程序中的粒子效果如何不同的问题。
Particle system looks different on iOS 6 and ios7
我最初是在ios7设备上设计的应用程序,让粒子看起来像我想要的,但在ios6设备上,粒子更小并且移位。
我被告知ios6和ios7之间存在问题,并且更改媒体播放时间可以解决问题。
我尝试了一切,但没有任何效果。
最终我尝试创建一个单独的应用并粘贴在本教程的代码中 - 视图中的http://weblog.invasivecode.com/post/45058779586/caemitterlayer-and-the-ios-particle-system-lets确实加载了。粒子可以正常工作,但是当我将相同的代码放入我当前的应用程序viewDidLoad时。这些颗粒遍布整个地方,并没有做到应该做的事情。
如果我当前的应用程序中有某些内容丢失,我会徘徊,这会让粒子感到不安。任何人都可以建议寻找的东西吗?
最初,粒子效果被子类化,并在将子视图添加到我的主视图后被调用。
这是我的视图控制器.h代码(我已经取出了一些不相关的控件对象属性和出口)
@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
以下是我的viewcontroller .m中的代码,其中我添加了教程中的代码。
@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;
}
这是我在ios7 iPad上运行的粒子效果之一,但在ios6 iPad上,效果看起来完全不同。
经过多长时间试图弄清楚为什么ios7和ios6看起来不同,我尝试将教程代码放入一个新的应用程序然后进入我自己的应用程序。在我的应用程序中,粒子是完全不同的,但它们应该在新的应用程序中。
关于@ matt的评论如下。我知道我的应用程序肯定会发生一些事情,但我无法看到它。我已经浏览了整个网络以及所有代码,我认为我错过了一些东西。
我想要的是三个粒子效果,无论iOS版本如何,都会出现在特定位置。其中一个用户触摸。以下效果只是其中之一。我已经尝试了CAmediaTiming的事情,但这没有用。
SteamEffect.h
#import <UIKit/UIKit.h>
@interface SteamEffect : UIView
@end
steamEffect.m看起来像这样。
#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
我使用以下命令从我的viewcontroller .m调用了上面的内容。上面的viewDidLoad中还有一个计时器,每隔5秒调用一次。虽然这可能不是这样做的。可能更好的是添加视图并开始每5秒发射一次,而不是每次都添加一个新视图,但这是一个不同的问题。
- (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];
}