利用UIWindow创建悬浮按钮


创建全局属性

@property(strong,nonatomic)UIWindow *giftWindow;
@property(strong,nonatomic)UIButton *giftBtn;
@property (strong,nonatomic) UIImageView *giftImgView;

创建按钮,window,imageView

- (void)giftButton {

    _giftWindow = [[UIWindow alloc]initWithFrame:CGRectMake(WIDTH-85, 30, 85, 85)];
    _giftWindow.windowLevel = UIWindowLevelAlert + 1;
    [_giftWindow makeKeyAndVisible];//关键语句,显示window

    _giftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 85, 85)];

    // 创建序列帧动画
    NSMutableArray *array = [NSMutableArray array];

    for (int i = 0; i < 7; i++) {
        NSString *imageName = [NSString stringWithFormat:@"lihe-%d", i + 1];
        UIImage *image = [UIImage imageNamed:imageName];

        // 把图片添加到数组中
        [array addObject:image];
    }

    self.giftImgView.animationImages = array;
    self.giftImgView.animationDuration = 1.5;
    self.giftImgView.animationRepeatCount = 0;    // 0 表示重复
    [self.giftImgView startAnimating];
    [_giftWindow addSubview:_giftImgView];

    _giftBtn = [[UIButton alloc] init];
    _giftBtn.backgroundColor = [UIColor clearColor];
    [_giftBtn addTarget:self action:@selector(giftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _giftBtn.frame = CGRectMake(0, 0, 85, 85);
    [_giftWindow addSubview:_giftBtn];

}

实现点击

- (void)giftBtnClick{
    NSLog(@"111");

    //让window消失
    [_giftWindow resignKeyWindow];
    _giftWindow = nil;   
}

PS:因为是新创建了一个window,所以跳到其他见面时也会有,如果不想让其他页面显示需要在viewDidDisappear里写

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [_giftWindow resignKeyWindow];
    _giftWindow = nil;
}

如果要回到本页面时再度显示,就在viewWillAppear里调用[self giftButton];即可

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self giftButton];
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值