自定义弹出消失视图

自定义弹出消失视图 

git 分支仓库

目录

自定义弹出消失视图... 1

1.     新建弹出视图ViewController 2

2.点击标题弹出控制器... 2

3.改变弹出View的frame. 3

4.改变弹出View的frame. 4

5. 弹出和消失动画代理的方法... 5

6. 改变titleBtn的状态... 6

 

  1. 新建弹出视图ViewController

import UIKit



class PopoverViewController: UIViewController {



    override func viewDidLoad() {

        super.viewDidLoad()

    }

}

2.点击标题弹出控制器

@objc private func titleBtnClick(titleBtn : TitleButton) {

        // 1.创建弹出的控制器

        let popoverVc = PopoverViewController()

       

        // 2.设置控制器的modal样式

        popoverVc.modalPresentationStyle = .custom

       

        // 3.设置转场的代理

        popoverVc.transitioningDelegate = popoverAnimator

        popoverAnimator.presentedFrame = CGRect(x: 100, y: 55, width: 180, height: 250)

       

        // 4.弹出控制器

        present(popoverVc, animated: true, completion: nil)

}

3.改变弹出View的frame

封装一个XMGPresentationController,继承自UIPresentationController转场动画ViewController。

import UIKit



class XMGPresentationController: UIPresentationController {

    // MARK:- 对外提供属性

    var presentedFrame : CGRect = CGRect.zero

   

    // MARK:- 懒加载属性

    private lazy var coverView : UIView = UIView()

   

    // MARK:- 系统回调函数

    override func containerViewWillLayoutSubviews() {

        super.containerViewWillLayoutSubviews()

       

        // 1.设置弹出View的尺寸

        presentedView?.frame = presentedFrame

       

        // 2.添加蒙版

        setupCoverView()

    }

}



// MARK:- 设置UI界面相关

extension XMGPresentationController {

    private func setupCoverView() {

        // 1.添加蒙版

        containerView?.insertSubview(coverView, at: 0)

       

        // 2.设置蒙版的属性

        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)

        coverView.frame = containerView!.bounds

       

        // 3.添加手势

        let tapGes = UITapGestureRecognizer(target: self, action: #selector(coverViewClick))

        coverView.addGestureRecognizer(tapGes)

    }

}



// MARK:- 事件监听

extension XMGPresentationController {

    @objc private func coverViewClick() {

        presentedViewController.dismiss(animated: true, completion: nil)

    }

}

4.改变弹出View的frame

// MARK:- 自定义转场代理的方法

extension PopoverAnimator : UIViewControllerTransitioningDelegate {

    // 目的:改变弹出View的尺寸

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {

        let presentation = XMGPresentationController(presentedViewController: presented, presenting: presenting)

        presentation.presentedFrame = presentedFrame

       

        return presentation

    }

   

    // 目的:自定义弹出的动画

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

       

        isPresented = true

        callBack!(isPresented)

       

        return self

    }

   

    // 目的:自定义消失的动画

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false

        callBack!(isPresented)

       

        return self

    }

   

}

 

5. 弹出和消失动画代理的方法

extension PopoverAnimator : UIViewControllerAnimatedTransitioning {

    /// 动画执行的时间

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 0.5

    }

   

    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View

    // UITransitionContextFromViewKey : 获取消失的View

    // UITransitionContextToViewKey : 获取弹出的View

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)

    }

   

    /// 自定义弹出动画

    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {

        // 1.获取弹出的View

        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

       

        // 2.将弹出的View添加到containerView中

        transitionContext.containerView.addSubview(presentedView)

      

        // 3.执行动画

        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)

        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

             presentedView.transform = CGAffineTransform.identity

        }) { (_) in

            // 必须告诉转场上下文你已经完成动画

            transitionContext.completeTransition(true)

        }

    }

   

    /// 自定义消失动画

    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {

        // 1.获取消失的View

        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)

       

        // 2.执行动画

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)

        }) { (_) in

            dismissView?.removeFromSuperview()

            // 必须告诉转场上下文你已经完成动画

            transitionContext.completeTransition(true)

        }

    }

}

6. 改变titleBtn的状态

1.自定义构造函数

// 注意:如果自定义了一个构造函数,但是没有对默认构造函数init()进行重写,那么自定义的构造函数会覆盖默认的init()构造函数

    init(callBack : @escaping (Bool) -> ()) {

        self.callBack = callBack

}

2.调用闭包

  // 目的:自定义弹出的动画

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

       

        isPresented = true

        callBack!(isPresented)

       

        return self

    }

   

    // 目的:自定义消失的动画

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false

        callBack!(isPresented)

       

        return self

}

3.调用初始化方法

private lazy var popoverAnimator : PopoverAnimator = PopoverAnimator {[weak self] (presented) -> () in

        self?.titleBtn.isSelected = presented

    }

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Android 自定义弹出框样式,可以按照以下步骤进行: 1. 创建一个自定义布局文件,包含弹出框的视图元素。 2. 创建一个继承自 Dialog 的类,重写其中的 onCreate 方法,在其中设置弹出框的样式和内容。 3. 在 Activity 中创建该自定义弹出框类的对象,并调用 show() 方法实现弹出框的显示。 下面是一个简单的自定义弹出框样式实现的示例代码: 1. 自定义布局文件 dialog_custom.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" android:textSize="18sp" android:textColor="#000000" android:gravity="center"/> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Input something..." android:textColor="#000000"/> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="OK" android:textColor="#FFFFFF" android:background="#009688" android:layout_marginTop="16dp"/> </LinearLayout> ``` 2. 自定义弹出框类 CustomDialog.java ```java public class CustomDialog extends Dialog { private TextView tvTitle; private EditText etInput; private Button btnOk; public CustomDialog(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_custom); tvTitle = findViewById(R.id.tv_title); etInput = findViewById(R.id.et_input); btnOk = findViewById(R.id.btn_ok); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在这里处理按钮点击事件 dismiss(); } }); // 设置弹出框样式 Window window = getWindow(); if (window != null) { window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); WindowManager.LayoutParams layoutParams = window.getAttributes(); layoutParams.gravity = Gravity.CENTER; layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(layoutParams); } } // 设置弹出框标题 public void setTitle(String title) { tvTitle.setText(title); } // 获取输入框的内容 public String getInputText() { return etInput.getText().toString(); } } ``` 3. 在 Activity 中使用自定义弹出框 ```java public class MainActivity extends AppCompatActivity { private CustomDialog customDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建自定义弹出框对象 customDialog = new CustomDialog(this); // 设置弹出框标题 customDialog.setTitle("Input Something"); // 显示弹出框 customDialog.show(); // 获取输入框的内容 String inputText = customDialog.getInputText(); } } ``` 这样就可以实现一个简单的自定义弹出框样式了。需要注意的是,弹出框的样式可以根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值