iOS 页面悬浮按钮 可拖动、自动吸附屏幕边缘 (OC)

上效果图

 

1.主要思路:首页建立一个悬浮Button给他加上移动手势(UIPanGestureRecognizer) 然后改变center传递给弹出页面;

2.代码

 .1初始化button

    self.xfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.xfBtn.frame = CGRectMake(0, 0, 44*1.6, 44*1.6);
    self.xfBtn.center = CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]);
    [self.view addSubview:self.xfBtn];
    [self.xfBtn addTarget:self action:@selector(jumXfAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.xfBtn setBackgroundImage:[UIImage imageNamed:@"sy_new_xf_center"] forState:UIControlStateNormal];
    

    UIPanGestureRecognizer *panG = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(xuanfuPanGesture:)];
    [self.xfBtn addGestureRecognizer:panG];

 .2处理手势

-(void)xuanfuPanGesture:(UIPanGestureRecognizer *)recognizer{
    if (self.xfBtn == recognizer.view) {
        CGPoint translation = [recognizer translationInView:self.view];
        CGFloat centerX = self.xfBtn.center.x+translation.x;
        self.xfBtn.center = CGPointMake(centerX, self.xfBtn.center.y+translation.y);
        [recognizer setTranslation:CGPointZero inView:self.view];
        CGFloat theCenterX = 0;
        CGFloat theCenterY = self.xfBtn.center.y;
        if (recognizer.state== UIGestureRecognizerStateEnded ||recognizer.state== UIGestureRecognizerStateCancelled) {
            if (centerX>SCREEN_WIDTH/2.0) {
                theCenterX = SCREEN_WIDTH-40;
            }else{
                theCenterX = 40;
            }
            if (self.xfBtn.center.y<[GetDataNum getStatusBarHight]+44+40) {
                theCenterY = [GetDataNum getStatusBarHight]+44+40;
            }
            if (self.xfBtn.center.y>SCREEN_HEIGHT-[GetDataNum getTabbarHeight]-([GetDataNum getStatusBarHight]+44)-40) {
                theCenterY = SCREEN_HEIGHT-[GetDataNum getTabbarHeight]-([GetDataNum getStatusBarHight]+44)-40;
            }
            
            [UIView animateWithDuration:0.3 animations:^{
                self.xfBtn.center = CGPointMake(theCenterX, theCenterY);
            }];
            
        }
        
    }
}

3.自定义alert

注意:弹出扇形菜单使用的是  HZYRoundShowButton    请取github上自行搜索下载

XuanFuBtnAlert.h

//
//  XuanFuBtnAlert.h
//  Zhouyi
//
//  Created by mac_os on 2022/7/8.
//

#import <UIKit/UIKit.h>
#import "HZYRoundShowButton.h"

NS_ASSUME_NONNULL_BEGIN

@interface XuanFuBtnAlert : UIView

@property(nonatomic,copy) void (^actBlock)(BOOL isSure);         //

@property (nonatomic, strong) UIView *contentView;

@property(nonatomic,strong) HZYRoundShowButton *roundBtn;

@property(nonatomic,assign) CGPoint centerPoint;         //
@property(nonatomic,assign) BOOL isLeft;         //


@property (nonatomic, assign) BOOL shouldDismissOnOutsideTapped;

- (void)showInView:(UIView *)view;

// Show in window
- (void)show;

// Dismiss the alert
- (void)dismiss;

@end

NS_ASSUME_NONNULL_END

XuanFuBtnAlert.m

#import "XuanFuBtnAlert.h"
#define IS_IOS_8_OR_HIGHER ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )

@interface XuanFuBtnAlert ()<HZYRoundShowButtonDelegate>
{
    CGRect ImgFrame;
}
@property (nonatomic, strong) UIView * alertContentView;
@property (nonatomic, strong) UIView * blackOpaqueView;


@property (nonatomic, assign) CGFloat alert_width;
@property (nonatomic, assign) CGFloat alert_height;

@property (nonatomic, assign) CGFloat dimAlpha;

@end

@implementation XuanFuBtnAlert

-(void)setCenterPoint:(CGPoint)centerPoint{
    _centerPoint = centerPoint;
}
-(void)setIsLeft:(BOOL)isLeft{
    _isLeft = isLeft;
}

- (id)initWithFrame:(CGRect)frame
{
    self.alert_width = SCREEN_WIDTH;
    self.alert_height = SCREEN_HEIGHT;

    self = [super initWithFrame:CGRectMake(0, 0, self.alert_width, self.alert_height)];
    if (self) {
        // Initialization code
        self.layer.cornerRadius = 8;
        self.clipsToBounds = YES;
        self.dimAlpha = 0.4;
//        [self calculateFrame];
        [self setupItems];
    }
    return self;
}

#pragma mark - Show the alert view
- (void)showInView:(UIView *)view
{
    [self calculateFrame];
    [self setupViews];
    
    UIView *window = [[[UIApplication sharedApplication] delegate] window];
    
    if (view != window) {

        self.blackOpaqueView = [[UIView alloc] initWithFrame:window.bounds];
        self.blackOpaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:self.dimAlpha];
        
        UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
        [self.blackOpaqueView addGestureRecognizer:outsideTapGesture];
        [view addSubview:self.blackOpaqueView];

    }
    [self addThisViewToView:view];
}

// Show in window
- (void)show
{
    
    UIView *superView;
    UIView *window = [[[UIApplication sharedApplication] delegate] window];
    if (IS_IOS_8_OR_HIGHER) {
        superView = window;
    } else {
        superView = [[window subviews] lastObject];
    }
    

    self.blackOpaqueView = [[UIView alloc] initWithFrame:window.bounds];
    self.blackOpaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:self.dimAlpha];

    UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
    [self.blackOpaqueView addGestureRecognizer:outsideTapGesture];
    [superView addSubview:self.blackOpaqueView];

    [self showInView:superView];
}

- (void)outsideTap:(UITapGestureRecognizer *)recognizer
{
    [self dismiss];
}


- (void) addThisViewToView: (UIView *) view
{
    NSTimeInterval timeAppear = .2;
    NSTimeInterval timeDelay = 0;

    [view addSubview:self];
    
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    self.alpha = .6;
    [UIView animateWithDuration:timeAppear delay:timeDelay options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 1;
        
    } completion:^(BOOL finished){
        [self didAppearAlertView];
    }];
    
}

// Hide and dismiss the alert
- (void)dismiss
{
    NSTimeInterval timeDisappear =  .08;
    NSTimeInterval timeDelay = .02;
    
    self.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:timeDisappear delay:timeDelay options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = .0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
    
    if (self.blackOpaqueView) {
        [UIView animateWithDuration:timeDisappear animations:^{
            self.blackOpaqueView.alpha = 0;
        } completion:^(BOOL finished) {
            [self.blackOpaqueView removeFromSuperview];
        }];
    }

}

-(void)setupItems{
    
}

//计算frame
-(void)calculateFrame{
        
    [self setUpItemsFrame];
}

-(void)setUpItemsFrame{
//
    NSMutableArray *arr_btn = [NSMutableArray array];
    for (int i = 1; i<=6; i ++) {
        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 48)];
        [btn1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"sy_new_xf_%d",i]] forState:UIControlStateNormal];
        btn1.tag = 200+i;
        [btn1 addTarget:self action:@selector(xuanfuAct:) forControlEvents:UIControlEventTouchUpInside];
        [arr_btn addObject:btn1];
    }
    
//    self.roundBtn = [HZYRoundShowButton roundShowButton:arr_btn andRadius:72 andSize:CGSizeMake(44, 44) andPosition:CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]+[GetDataNum getStatusBarHight]+44)];
//    self.xfBtn.center = CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]);
    self.roundBtn = [HZYRoundShowButton roundShowButton:arr_btn andRadius:72 andSize:CGSizeMake(44, 44) andPosition:CGPointMake(self.centerPoint.x, self.centerPoint.y+[GetDataNum getStatusBarHight]+44)];

    if (self.isLeft) {
        self.roundBtn.arcLength = M_PI+M_PI/4;
        self.roundBtn.startAngle = M_PI+M_PI/2;
    }else{
        self.roundBtn.arcLength = M_PI+M_PI/4;
        self.roundBtn.startAngle = M_PI / 2;
    }
    self.roundBtn.roundButton = YES;
    self.roundBtn.delegate = self;
    self.roundBtn.backgroundColor = [UIColor clearColor];
    
}
//更新试图
- (void)setupViews
{
    // Setup Background
   if (self.backgroundColor) {
        [self setBackgroundColor:self.backgroundColor];
    } else {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    
//  //由于self是全屏大小
    UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
    [self addGestureRecognizer:outsideTapGesture];

    [self addSubview:self.roundBtn];
    
    [self.roundBtn showBtn];
}


//已经出现的逻辑
-(void)didAppearAlertView{
    
    
}


-(void)jumpACtion:(UIButton *)btn{
    
    if (self.actBlock) {
        self.actBlock(YES);
    }
    [self dismiss];
}

-(void)xuanfuAct:(UIButton *)btn{
    
    [self dismiss];
}

- (void)baseButtonDidTouched:(UIButton *)baseBtn widthOpened:(BOOL)isOppened{
    [self dismiss];
}


@end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下代码来实现 iOS 按钮的背景渐变色:CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = self.btn.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil]; [self.btn.layer insertSublayer:gradient atIndex:0]; ### 回答2: iOS按钮背景渐变色可以通过以下步骤使用OC代码实现: 1. 导入渐变色所需的头文件: ```objc #import <QuartzCore/QuartzCore.h> ``` 2. 创建按钮: ```objc UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; [self.view addSubview:myButton]; ``` 3. 创建渐变色图层: ```objc CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = myButton.bounds; ``` 4. 设置渐变色图层的颜色: ```objc gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor]; ``` 5. 设置渐变色方向: ```objc // 横向渐变 gradient.startPoint = CGPointMake(0, 0.5); gradient.endPoint = CGPointMake(1, 0.5); // 纵向渐变 // gradient.startPoint = CGPointMake(0.5, 0); // gradient.endPoint = CGPointMake(0.5, 1); ``` 6. 将渐变色图层添加到按钮的背景图层中: ```objc [myButton.layer insertSublayer:gradient atIndex:0]; ``` 7. 最后,可以给按钮设置一些其他属性,如标题、字体等: ```objc [myButton setTitle:@"渐变按钮" forState:UIControlStateNormal]; [myButton.titleLabel setFont:[UIFont systemFontOfSize:17]]; [myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; ``` 通过以上步骤,即可通过OC代码实现iOS按钮的背景渐变色效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值