首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画停止后移除UIButton

动画停止后移除UIButton
EN

Stack Overflow用户
提问于 2011-08-27 23:00:06
回答 2查看 529关注 0票数 0

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

代码语言:javascript
复制
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)];
}

我最终做了:

代码语言:javascript
复制
button.tag = uniqueID;
[pathAnimation setValue:[NSString stringWithFormat:@"%d", uniqueID] forKey:@"name"];

然后:

代码语言:javascript
复制
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString* name = [theAnimation valueForKey:@"name"];
[self removeImage:[myView viewWithTag:[name intValue]]];
[name release];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-28 00:41:41

您可以只使用一个简单的标记,就像这样....

创建按钮的

代码语言:javascript
复制
#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];

删除按钮

代码语言:javascript
复制
[[self.view viewWithTag:kButtonTag] removeFromSuperview];
票数 1
EN

Stack Overflow用户

发布于 2011-08-28 00:37:07

您可以简单地调用

代码语言:javascript
复制
[button removeFromSuperview];

在动画代码的末尾。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7215318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档