我在粒子效果方面遇到了一些困难。最初我发布了一个关于我的应用程序中的粒子效果在 ios6 设备上有何不同的问题。
我最初在 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];
}