0

我在 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");

}

4

1 回答 1

1

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

@interface MyViewController : UIViewController {
    UIView *_glowView;
}

...

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

然后在您的didTapButton:方法中,您可以像处理水龙头一样处理水龙头(添加或删除_glowView,但请确保您没有将其设置为,nil以便始终保留对它的引用)。这样,您只需创建和添加一次,问题就应该解决了。

于 2013-07-18T05:30:02.677 回答