首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调光器调光按钮

调光器调光按钮
EN

Stack Overflow用户
提问于 2012-11-20 14:40:27
回答 3查看 186关注 0票数 0

我正在尝试循环四张亮度稍高的图像,我试图让它看起来像是在发光,但是我认为代码并没有像我想象的那样工作

代码语言:javascript
复制
  -(void)loadRefreshButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    refreshButton = button;
    [button addTarget:self
               action:@selector(refreshButtonPressedResult)
     forControlEvents:UIControlEventTouchDown];    [refreshButton setTitle:@"" forState:UIControlStateNormal];
    refreshButton.frame = CGRectMake(80.0, 370.0, 76, 76);
    [self.view addSubview:refreshButton];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                      target: self
                                                    selector: @selector(toggleButtonImage:)
                                                    userInfo: nil
                                                     repeats: YES];
}


    - (void)toggleButtonImage:(NSTimer*)timer
{


     if(toggle)
        {
        [refreshButton setImage:[UIImage imageNamed:@"button_zero.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_one.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_two.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_three.png"] forState: UIControlStateNormal];
        }
    else
        {
        [refreshButton setImage:[UIImage imageNamed:@"button_three.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_two.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_one.png"] forState: UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"button_zero.png"] forState: UIControlStateNormal];
        }
    toggle = !toggle;
}

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-20 15:25:53

也许UIView类方法animateWithDuration会达到预期的效果。这很简单:

代码语言:javascript
复制
[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     refreshButton.alpha = 0.75;
                 }
                 completion:nil];

显然,请相应地调整durationalpha以达到预期效果。

票数 1
EN

Stack Overflow用户

发布于 2012-11-20 14:57:27

你不需要有四个图像。您可以拥有更多亮度的单个图像,您可以使用以下代码来创建闪烁动画。

代码语言:javascript
复制
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0.3]];
    [animation setDuration:0.8f];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setAutoreverses:YES];
    [animation setRepeatCount:INFINITY];
    [[aButton layer] addAnimation:animation forKey:@"opacity"];
票数 0
EN

Stack Overflow用户

发布于 2012-11-20 15:13:36

只需添加以下代码

代码语言:javascript
复制
- (void)toggleButtonImage:(NSTimer*)timer
{
        ///write your code above which define above after at last just add this code..
        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];   
        [animation setType:kCATransitionFade];
        [animation setDuration:0.5];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                      kCAMediaTimingFunctionEaseInEaseOut]];
        [[refreshButton layer] addAnimation:animation forKey:@"transitionViewAnimation"];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13467843

复制
相关文章

相似问题

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