2011-08-27 35 views
0

我正在向UIView添加一個按鈕並創建一個動畫。動畫停止時如何移除按鈕?這裏是我的代碼...動畫停止後刪除UIButton

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]; 
} 

回答

1

你可以只使用一個簡單的標籤,像這樣的....

創建按鈕

#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]; 
+0

感謝。我結束了: – user472292

0

你可以簡單地調用

[button removeFromSuperview]; 

在年底你的動畫代碼。