动画停止后删除 UIButton

发布于 2024-12-02 02:57:52 字数 2638 浏览 0 评论 0原文

我正在向 UIView 添加一个按钮并创建一个动画。动画停止时如何删除按钮?这是我的代码...

if (myImageCounter < MAX_IMAGES)
{
    // Set Counter

    myImageCounter++;

    // Add Image

    OBShapedButton *button = [OBShapedButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"images/Happy.gif"];
    int width = [self randomNumberBetween:MAX_WIDTH and:MIN_WIDTH];
    int height = width;

    // Set button properties
    button.adjustsImageWhenHighlighted = NO;
    button.adjustsImageWhenDisabled = NO;
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, width, height);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    // Add button to view
    [self.view addSubview:button];

    // Animate image

    int adjustedMaxX = MAX_X - height;
    int adjustedMaxY = MAX_Y - width;
    int x0 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y0 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x1 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y1 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x2 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y2 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x3 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y3 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];

    // Create animage
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 1;

    // Create path
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, x0, y0);
    CGPathAddCurveToPoint(curvedPath, NULL, x1, y1, x2, y2, x3, y3);

    // Set path
    pathAnimation.path = curvedPath;

    // Draw path
    [myView drawPath:curvedPath];

    // Run animation
    [button.layer addAnimation:pathAnimation forKey:nil];

    // Release path
    CGPathRelease(curvedPath);  

    // Increment image count
    [self setImage:(myImageCounter + 1)];
}

我最终做了:

button.tag = uniqueID;
[pathAnimation setValue:[NSString stringWithFormat:@"%d", uniqueID] forKey:@"name"];

然后:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString* name = [theAnimation valueForKey:@"name"];
[self removeImage:[myView viewWithTag:[name intValue]]];
[name release];
}

I am adding a button to a UIView and creating an animation. How do I remove the button when the animation stops? Here is my code...

if (myImageCounter < MAX_IMAGES)
{
    // Set Counter

    myImageCounter++;

    // Add Image

    OBShapedButton *button = [OBShapedButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"images/Happy.gif"];
    int width = [self randomNumberBetween:MAX_WIDTH and:MIN_WIDTH];
    int height = width;

    // Set button properties
    button.adjustsImageWhenHighlighted = NO;
    button.adjustsImageWhenDisabled = NO;
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, width, height);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    // Add button to view
    [self.view addSubview:button];

    // Animate image

    int adjustedMaxX = MAX_X - height;
    int adjustedMaxY = MAX_Y - width;
    int x0 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y0 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x1 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y1 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x2 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y2 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];
    int x3 = [self randomNumberBetween: adjustedMaxX and: MIN_X];
    int y3 = [self randomNumberBetween: adjustedMaxY and: MIN_Y];

    // Create animage
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 1;

    // Create path
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, x0, y0);
    CGPathAddCurveToPoint(curvedPath, NULL, x1, y1, x2, y2, x3, y3);

    // Set path
    pathAnimation.path = curvedPath;

    // Draw path
    [myView drawPath:curvedPath];

    // Run animation
    [button.layer addAnimation:pathAnimation forKey:nil];

    // Release path
    CGPathRelease(curvedPath);  

    // Increment image count
    [self setImage:(myImageCounter + 1)];
}

I ended up doing:

button.tag = uniqueID;
[pathAnimation setValue:[NSString stringWithFormat:@"%d", uniqueID] forKey:@"name"];

Then:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString* name = [theAnimation valueForKey:@"name"];
[self removeImage:[myView viewWithTag:[name intValue]]];
[name release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-09 02:57:52

您可以只使用一个简单的标签,如下所示...

创建按钮

#define  kButtonTag  1

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(50.0, 50.0, 220.0, 80.0)];
[myButton setTag:kButtonTag];
[myButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

删除按钮

[[self.view viewWithTag:kButtonTag] removeFromSuperview];

You could just use a simple tag, like this....

Creating the button

#define  kButtonTag  1

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(50.0, 50.0, 220.0, 80.0)];
[myButton setTag:kButtonTag];
[myButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

Removing the button

[[self.view viewWithTag:kButtonTag] removeFromSuperview];
御弟哥哥 2024-12-09 02:57:52

您可以简单地

[button removeFromSuperview];

在动画代码末尾调用。

You may simply call

[button removeFromSuperview];

at the end of your animation code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文