gpt4 book ai didi

ios - UIView 发出匹配边框形状的环

转载 作者:行者123 更新时间:2023-11-28 22:27:10 25 4
gpt4 key购买 nike

我一直在尝试在 iOS 中创建一个从边框发出圆环的自定义 View 。我使用 Core Graphics 绘制了一个椭圆,我希望它周期性地散发。

我一直在寻找同时使用 CAEmitterLayer 和 Core Animation 来实现这一点,但真的不知道如何实现我想要的效果。理想情况下,我希望椭圆从形状的边缘发出一个大约 10 像素厚的环,并随着它的增长和逐渐远离而逐渐消失。

对于我最初的尝试,我只是使用 Core Animation 让椭圆每 3 秒增长和淡化,但我真正想要的是让椭圆保持静止并有另一个动画层。

任何建议都会很棒。

我的代码已经从最初的尝试开始。但是我的 atm 是:

#import "ThumbView.h"

#import <QuartzCore/QuartzCore.h>

@implementation ThumbView

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {

[super drawLayer:layer inContext:ctx];

// Create the emitter layer and make it the same size as the view.
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.frame = self.frame;

// Apply some attributes to the emitter.
emitterLayer.emitterShape = kCAEmitterLayerCircle;
emitterLayer.renderMode = kCAEmitterLayerOutline;
emitterLayer.emitterSize = CGSizeMake ( 4, 4 );

// Create a scale animation that repeats every 3 seconds.
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration = 3.0f;
scaleAnimation.repeatCount = HUGE_VAL;
scaleAnimation.values = @[@1, @1.5f, @1.5f];
scaleAnimation.keyTimes = @[@0, @0.5, @1];

// Create a fade animation that repeats every 3 seconds.
CAKeyframeAnimation *glowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
glowAnimation.duration = 3.0f;
glowAnimation.repeatCount = HUGE_VAL;
glowAnimation.values = @[@1, @0, @0];
glowAnimation.keyTimes = @[@0, @0.5, @1];

[emitterLayer addAnimation:scaleAnimation forKey:@"scale"];
[emitterLayer addAnimation:glowAnimation forKey:@"opacity"];

if ( !self.pressed ) {
[layer insertSublayer:emitterLayer above:layer];
} else {
[emitterLayer removeFromSuperlayer];
}

}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

// Drawing code. Draw an elipse here.
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor ( context, self.thumbColour.CGColor );
CGContextFillEllipseInRect ( context, rect );

}


@end

所以我一直在尝试做的是在 drawLayer:inContext: delegate 方法中创建发射器层;对其应用动画;并将其作为子层添加到 View 层。

最佳答案

我会建议使用一个或多个 CAShapeLayer 对象,并为安装在形状层中的 CGPath 的比例设置动画。

默认情况下,形状图层为其路径更改设置动画。

您应该能够创建一组形状图层来描边您要绘制的椭圆,并更改这些路径的比例设置以获得您想要的效果。

关于ios - UIView 发出匹配边框形状的环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com