【问题标题】:Show blinking control on top of uinavigationbar in iOS在 iOS 的 uinavigationbar 顶部显示闪烁控件
【发布时间】:2015-06-06 23:22:38
【问题描述】:

我想在后台调用时在我的应用中实现与 iOS 提供程序相同的功能。

即在某些情况下,我想在应用程序的屏幕顶部(在状态栏和导航栏上)显示一个闪烁的按钮,当用户点击该按钮时,我想将特定的视图控制器推到相同的位置。

参考截图

实现这一目标的最佳解决方案是什么?

我试过了

 UIButton *hideBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; 
[hideBtn addTarget:delegate.familyJobMonitorViewController action:@selector(hideAndShowView) forControlEvents:UIControlEventTouchUpInside];
    [delegate.navigationController.navigationBar addSubview:hideBtn];

它工作正常,但在这种情况下我必须手动管理很多事情。有没有更好的呢

直接 API 或可可内置的东西。

【问题讨论】:

    标签: ios objective-c cocoa-touch ios7 uinavigationcontroller


    【解决方案1】:

    您可以使用CoreAnimation 循环动画,在这种情况下切换图层的不透明度。通过设置autoreverses = true,动画将在fromValuetoValue 之间循环。通过设置repeatCount = MAXFLOAT,动画将一直运行直到停止。

    @implementation ViewController
    {
        BOOL        _blinking;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"Tap to Resume" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(onResume:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.titleView = button;
    }
    
    -(IBAction)onResume:(id)sender
    {
        [self toggleBlinking];
    }
    
    -(void)toggleBlinking
    {
        CALayer*    layer = self.navigationItem.titleView.layer;
    
        _blinking = !_blinking;
    
        if(!_blinking)
        {
            [layer removeAnimationForKey:@"opacity"];
        }
        else
        {
            CABasicAnimation*    animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            animation.fromValue = @(1.0);
            animation.toValue = @(0.0);
            animation.duration = 1.0;
            animation.autoreverses = true;
            animation.repeatCount = MAXFLOAT;
    
            [layer addAnimation:animation forKey:@"opacity"];
        }
    }
    
    @end
    

    【讨论】:

    • 我想在已经有另一个标题的导航栏上添加这个。所以设置标题视图是行不通的。但我可以使用这个动画代码。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多