为什么这只会消失并且不会消失

时间:2013-07-17 09:48:53

标签: ios5 ios6

我在UIButton上有一个图像,当我点击按钮时我会发光,当我再次点击它时会发光。

每次点击时,它都不会发光,只会让它变得更亮 。 我尝试过UIView animateWithDuration以及CATransition和CABasicAnimation ..

出了点问题。可能是我应该知道的基本内容。

我很感激帮助。

继承我的代码:

-(void)didTapButton:(UIButton *)button {

button.selected = !button.selected;

UIColor *glowColor = [self.arrayOfGlowColor objectAtIndex:button.tag];
UIImageView *imageView = button.imageView;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,imageView.bounds.size.width,imageView.bounds.size.height)];
[glowColor setFill];
[path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];

UIImage *imageContext_image = UIGraphicsGetImageFromCurrentImageContext();

UIView *glowView = [[UIImageView alloc] initWithImage:imageContext_image];
glowView.center = imageView.center;
[imageView.superview insertSubview:glowView aboveSubview:imageView];

glowView.alpha = 0;
glowView.layer.shadowColor = glowColor.CGColor;
glowView.layer.shadowOffset = CGSizeZero;
glowView.layer.shadowRadius = 10;
glowView.layer.shadowOpacity = 1.0;

/*
[UIView animateWithDuration: 1.0 animations:^(void) {
    glowView.alpha = (button.selected) ? 1.0 : 0.0f;
}];
NSLog(@"glowView.alpha:%f",glowView.alpha);
NSLog(button.selected ? @"YES" : @"NO");
*/
/*


if (button.selected) {
    CATransition *fadeIn = [CATransition animation];
    fadeIn.duration = 1.0;
    fadeIn.startProgress = 0.0f;
    fadeIn.endProgress = 1.0f;
    [fadeIn setType:kCATransitionFade];
    fadeIn.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [fadeIn setFillMode:@"extended"];
    [glowView.layer addAnimation:fadeIn forKey:nil];

}

if (!button.selected) {
    CATransition *fadeOut = [CATransition animation];
    fadeOut.duration = 1.0;
    fadeOut.startProgress = 1.0f;
    fadeOut.endProgress = 0.0f;

    [fadeOut setType:kCATransitionFade];
    fadeOut.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [fadeOut setFillMode:@"extended"];
    [glowView.layer addAnimation:fadeOut forKey:nil];

}
*/


if (button.selected) {

//Create a one-way animation that fades in the glow.
    glowView.layer.opacity = 1.0;
CABasicAnimation *glowFadeInAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
glowFadeInAnim.fromValue = [NSNumber numberWithDouble:0.0];
glowFadeInAnim.toValue = [NSNumber numberWithDouble:1.0];

glowFadeInAnim.repeatCount = 1;
glowFadeInAnim.duration = 1.0;
glowFadeInAnim.autoreverses = FALSE;
    glowFadeInAnim.fillMode = kCAFillModeForwards;
    glowFadeInAnim.removedOnCompletion = NO;
glowFadeInAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];


[glowView.layer addAnimation:glowFadeInAnim forKey:nil];
}


if (!button.selected) {
            glowView.layer.opacity = 1.0;
    CABasicAnimation *glowFadeOutAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    glowFadeOutAnim.fromValue = [NSNumber numberWithDouble:1.0];
    glowFadeOutAnim.toValue = [NSNumber numberWithDouble:0.0];

    glowFadeOutAnim.repeatCount = 1;
    glowFadeOutAnim.beginTime = 2;
    glowFadeOutAnim.duration = 2.0;
    glowFadeOutAnim.autoreverses = FALSE;
    glowFadeOutAnim.fillMode = kCAFillModeForwards;
    glowFadeOutAnim.removedOnCompletion = NO;
    glowFadeOutAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];


    [glowView.layer addAnimation:glowFadeOutAnim forKey:nil];
            glowView.layer.opacity = 0.0;
}

NSLog(@"glowView.opacity:%f",glowView.layer.opacity);
NSLog(button.selected ? @"YES" : @"NO");

}

1 个答案:

答案 0 :(得分:1)

如果每按一次按钮创建一个新的glowView并将其添加到按钮,那么显然它会越来越亮,因为越来越多的按钮被添加到按钮中。您需要创建glowView 一次,例如在viewDidLoad方法中,并将其存储为实例变量,这样您就不会丢失它:

@interface MyViewController : UIViewController {
    UIView *_glowView;
}

...

- (void)viewDidLoad {
    [super viewDidLoad];
    _glowView = ... // Initialise it here
    ...
    [imageView.superview insertSubview:_glowView aboveSubview:imageView];
}

然后在您的didTapButton:方法中,您可以像处理一样点击(添加或删除_glowView,但请确保不要将其设置为nil,以便参考它始终保留)。这样,您只需创建并添加一次,问题就应该解决了。

相关问题