闪耀效果iOS

我想在图像上创建闪光效果,如视频 Sparkle Effect所示
我能想到的唯一方法是使用核心动画分别为每个粒子制作动画,但这样既低效又耗时.
有没有其他方法可以做同样的事情?

解决方法

以下是Erica Susan的烹饪书中的解决方案.看到这个适合你.

You can add visual interest to your interfaces by using emitters in tandem with user touches. The following class demonstrates how to follow a touch over its lifetime,adding a little @R_29_4502@ to wherever the user touches on-screen.

The class begins as soon as the user touches the screen,creating an emitter layer and a single emitter cell. The cell defines the particles — their color,their birth rate,their lifetime,veLocity,and so forth.

As the user’s touch progresses,this class updates the emitter’s location,removing the emitter once the touch is removed from the screen. Although this example is written for single touch interaction,you can easily update the code to add an array of emitters (rather than a single instance) for multi-touch interaction.

Emitters are easily added to your projects and efficient to run. While too much animation is never a good design idea,a little @R_29_4502@ used judicIoUsly can add life and movement.

@interface @R_29_4502@TouchView : UIView {
    CAEmitterLayer *emitter;
}

@end

@implementation @R_29_4502@TouchView

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    float multiplier = 0.25f;

    CGPoint pt = [[touches anyObject] locationInView:self];

    //Create the emitter layer
    emitter = [CAEmitterLayer layer];
    emitter.emitterPosition = pt;
    emitter.emitterMode = kCAEmitterLayerOutline;
    emitter.emitterShape = kCAEmitterLayerCircle;
    emitter.renderMode = kCAEmitterLayerAdditive;
    emitter.emitterSize = CGSizeMake(100 * multiplier,0);

    //Create the emitter cell
    CAEmitterCell* particle = [CAEmitterCell emitterCell];
    particle.emissionLongitude = M_PI;
    particle.birthRate = multiplier * 1000.0;
    particle.lifetime = multiplier;
    particle.lifetimeRange = multiplier * 0.35;
    particle.veLocity = 180;
    particle.veLocityRange = 130;
    particle.emissionRange = 1.1;
    particle.scaleSpeed = 1.0; // was 0.3
    particle.color = [[COOKBOOK_PURPLE_COLOR colorWithAlphaComponent:0.5f] CGColor];
    particle.contents = (__bridge id)([UIImage imageNamed:@"spark.png"].CGImage);
    particle.name = @"particle";

    emitter.emitterCells = [NSArray arrayWithObject:particle];
    [self.layer addSublayer:emitter];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self];

    // disable implicit animations
    [CATransaction begin];
    [CATransaction setValue:(id)kcfBooleanTrue forKey:kCATransactiondisableActions];
    emitter.emitterPosition = pt;    
    [CATransaction commit];    
}

 - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [emitter removeFromSuperlayer];
    emitter = nil;
 }

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

@end

不要忘记创建一个名为spark.png的png文件来创建动画.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...