gpt4 book ai didi

ios - 基于iOS定位的图层蒙版

转载 作者:行者123 更新时间:2023-12-01 19:08:49 24 4
gpt4 key购买 nike

我从未专门在 iOS 上使用过动画,希望能指出正确的方向。到目前为止,我已经以类似于下面的方式为一堆药丸状对象设置了动画,但是我还没有根据形状的 y 位置实现正确的 mask 。

sine wave

药丸形状只是 UIView。我正在寻找一种方法来根据他们的定位改变他们的背景;背景本身不动,只是药丸。

编辑:

根据下面 Rob 的回答,我现在将动画与设备的运动联系起来,如下所示:

- (void)setAnimationToDeviceMotion {
motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 0.1f;
NSOperationQueue *motionQueue = [[NSOperationQueue alloc] init];

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical toQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
if (!error) {
double pitch = motion.attitude.pitch;
NSLog(@"%f", pitch);
[self addMaskToView:self.imageView withAdjustment:pitch];
}
}];
}

最佳答案

假设这是八个药丸形状,下面会显示一个图像,你

  • 创建 UIBezierPath用于八种药丸形状
  • 创建 CAShapeLayer使用它的层UIBezierPath (或更准确地说,它的 CGPath )
  • 应用此CAShapeLayer作为 mask对于layer为您的UIImageView

  • 如果您想随着时间的推移对其进行动画处理,只需替换 mask (例如,响应加速度计事件, CADisplayLinkNSTimerUIPanGesture ),或者只是更新 pathCAShapeLayer您已将掩码应用到 UIImageView的层。

    因此,您可能有类似的东西:
    - (void)addMaskToView:(UIView *)view withAdjustment:(CGFloat)adjustment
    {
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = [[self pillsPathWithAdjustment:adjustment forView:view] CGPath];

    self.imageView.layer.mask = mask;
    }

    - (UIBezierPath *)pillsPathWithAdjustment:(CGFloat)adjustment forView:(UIView *)view
    {
    UIBezierPath *path = [UIBezierPath bezierPath];

    for (NSInteger i = 0; i < kNumberOfPills; i++)
    [self addPillPathNumber:i forView:view toPath:path adjustment:adjustment];

    return path;
    }

    - (void)addPillPathNumber:(NSInteger)index forView:(UIView *)view toPath:(UIBezierPath *)path adjustment:(CGFloat)adjustment
    {
    CGFloat const pillSpacing = 5.0f;
    CGFloat const pillWidth = view.bounds.size.width / kNumberOfPills - pillSpacing;
    CGFloat const pillHeight = view.bounds.size.height * 0.4;
    CGFloat const cornerRounding = 10.0f;

    CGPoint point = CGPointMake(index * (pillWidth + pillSpacing) + pillSpacing / 2.0, sinf((float) index * M_PI / kNumberOfPills + adjustment * M_PI * 2.0) * 100.0);

    // top

    point.x += cornerRounding;
    [path moveToPoint:point];
    point.x += pillWidth - cornerRounding * 2.0;
    [path addLineToPoint:point];

    // top right corner

    [path addArcWithCenter:CGPointMake(point.x, point.y + cornerRounding) radius:cornerRounding startAngle:3 * M_PI_2 endAngle:2.0 * M_PI clockwise:YES];
    point.x += cornerRounding;
    point.y += cornerRounding;

    // right

    point.y += pillHeight - cornerRounding * 2.0;
    [path addLineToPoint:point];

    // lower right corner

    [path addArcWithCenter:CGPointMake(point.x - cornerRounding, point.y) radius:cornerRounding startAngle:0 endAngle:M_PI_2 clockwise:YES];

    // bottom

    point.y += cornerRounding;
    point.x -= cornerRounding;
    point.x -= pillWidth - cornerRounding * 2.0;
    [path addLineToPoint:point];

    // lower left corner

    [path addArcWithCenter:CGPointMake(point.x, point.y - cornerRounding) radius:cornerRounding startAngle:M_PI_2 endAngle:M_PI clockwise:YES];

    // left

    point.y -= cornerRounding;
    point.x -= cornerRounding;
    point.y -= pillHeight - cornerRounding * 2.0;
    [path addLineToPoint:point];

    // upper left corner

    [path addArcWithCenter:CGPointMake(point.x + cornerRounding, point.y) radius:cornerRounding startAngle:M_PI endAngle:3.0 * M_PI_2 clockwise:YES];

    [path closePath];
    }

    举例来说,这就是我使用 CADisplayLink 设置动画的方式。 :
    - (void)startDisplayLink
    {
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    self.startTime = CACurrentMediaTime();
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }

    - (void)stopDisplayLink
    {
    [self.displayLink invalidate];
    self.displayLink = nil;
    }

    - (void)handleDisplayLink:(CADisplayLink *)displayLink
    {
    CGFloat seconds;
    CGFloat fractionsOfSecond = modff(CACurrentMediaTime() - self.startTime, &seconds);

    [self addMaskToView:self.imageView withAdjustment:fractionsOfSecond];
    }

    关于ios - 基于iOS定位的图层蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18158896/

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