弹出视图

开发时经常会遇到一些,在点击某个按钮弹出一个视图,视图浮于现视图的上面的情形,总结一下:


思路:

  • 1.自定义一个view,比如叫popView,它继承于UIView而创建,popView即弹出视图,在这个自定义的popView里面,可以写入弹出视图里面的一些内容。

  • 2.由于这个弹出视图是由于某个Controller里面的某个按钮触发的方法实现的,所以在触发的方法里面,实现这个视图的创建。

  • 3.另外,特别注意,弹出视图上还应有个按钮,点击这个按钮,弹出视图则消失了,由于这个按钮是在自定义的popView中添加的,而这个按钮触发的使得这个弹出视图消失的方法,却无法在这里面实现,所以可以通过一个代理方法实现,即:popView是委托方,Controller是代理方,当popView中的按钮被点击了时,Controller就将这个弹出视图移除掉。

代码如下:

//PopupView的.h文件
@protocol popupDelegate <NSObject>

- (void)popUpRemove;//点击弹出视图的按钮时触发的使得弹出视图消失的方法

@end

@interface PopupView : UIView

@property (nonatomic,assign) id<popupDelegate>delegate;

@end
#import "PopupView.h"//PopupView的.m文件

@implementation PopupView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

//        self.center = self.superview.center;
        CGRect bounds = [[UIScreen mainScreen]bounds];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y+5, self.frame.size.width/3, 20)];
    label.text = @"说明";
    label.font = [UIFont systemFontOfSize:15];

    [self addSubview:label];

    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.origin.x+200, self.frame.origin.y+5,15,15)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"xiao"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    }
    return self;
}




- (void)back:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(popUpGoTo)]) {

         [self.delegate popUpGoTo];
    }
}

import “QuotedListViewController.h”

import “AFNetworking.h”

import “QuotedListTableViewCell.h”

import “OrderModel.h”

import “MyNavigationButton.h”

import “PopupView.h”

define key_productId @”productId”//商品ID

define key_productName @”productName”

define key_publishCount @”publishCount”

define key_count @”count”

define key_price @”price”

@interface QuotedListViewController (){
PopupView *_infoView;
NSMutableArray *dataArray; //加入了用于保存数组的数组 dataArray
//保存每次修改的数据;
NSMutableArray *_sendArray;

NSArray *_keyArray;

}

import “ViewController.h”

import “PopupView.h”

@interface QuotedListViewController (){
PopupView *_infoView;

}

  • (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
    [btn setTitle:@”弹出视图按钮” forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:20];
    [btn addTarget:self action:@selector(popUp:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    }

  • (void)popUp:(UIButton *)sender
    {
    if (_infoView == nil) {
    CGRect bounds = [[UIScreen mainScreen]bounds];

    self.view.alpha = 0.5f;
    
    _infoView= [[PopupView alloc]initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, 230, 260)];
    _infoView.delegate = self;
    CGPoint centerPoint=CGPointMake(self.view.center.x, 300);
    
    _infoView.center = centerPoint;
    [[UIApplication sharedApplication].keyWindow addSubview:_infoView];//加到主窗口上面
    

    }else
    {self.view.alpha = 0.5f;
    [[UIApplication sharedApplication].keyWindow addSubview:_infoView];
    }

}

pragma mark 代理实现方法

  • (void)popUpRemove
    {
    if (_infoView.superview) {
    self.view.alpha = 1.0f;
    [_infoView removeFromSuperview];//视图从父视图移除 (如果_infoView的父视图存在,就将它从父视图中移除)
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由访问、修改和定制操作系统,这促进了技术的创新和发展,使得Android系统具有高度的灵活性和可定制性。 多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第三方应用市场下载和安装各种各样的应用程序,满足各种需求。 可定制性:Android操作系统可以根据用户的个人喜好进行定制,用户可以更改主题、小部件和图标等,以使其界面更符合个人风格和偏好。 多种设备支持:Android操作系统可以运行在多种不同类型的设备上,包括手机、平板电脑、智能电视、汽车导航系统等。 此外,Android系统还有一些常见的问题,如应用崩溃、电池耗电过快、Wi-Fi连接问题、存储空间不足、更新问题等。针对这些问题,用户可以尝试一些基本的解决方法,如清除应用缓存和数据、降低屏幕亮度、关闭没有使用的连接和传感器、限制后台运行的应用、删除不需要的文件和应用等。 随着Android系统的不断发展,其功能和性能也在不断提升。例如,最新的Android版本引入了更多的安全性和隐私保护功能,以及更流畅的用户界面和更强大的性能。此外,Android系统也在不断探索新的应用场景,如智能家居、虚拟现实、人工智能等领域。 总之,Android系统是一种功能强大、灵活可定制、拥有丰富应用生态系统的移动操作系统,在全球范围内拥有广泛的用户基础。
在iOS的开发中,使用Swift可以实现底部弹出视图。以下是一个简单的实现方式: 首先,需要创建一个底部弹出视图的ViewController。可以在Storyboard中创建一个新的ViewController,并设置其底部约束为屏幕底部,高度为底部弹出视图的高度。可以在该ViewController中添加需要显示的内容,比如按钮、标签等。 然后,在需要弹出底部视图的地方,可以通过 present 方法来显示这个ViewController。可以在当前的ViewController中添加一个按钮,当点击该按钮时,调用如下代码来显示底部弹出视图: ```swift // 创建底部弹出视图的ViewController let bottomViewController = storyboard?.instantiateViewController(withIdentifier: "BottomViewController") as! BottomViewController // 设置底部弹出视图的 ModalPresentationStyle 为 .overCurrentContext,使得弹出视图会覆盖当前视图 bottomViewController.modalPresentationStyle = .overCurrentContext // 设置底部弹出视图的 TransitioningDelegate;通过实现相关代理方法来自定义弹出视图的动画效果 bottomViewController.transitioningDelegate = self // 弹出底部视图 present(bottomViewController, animated: true, completion: nil) ``` 在当前ViewController中,需要增加一个扩展来实现底部弹出视图的自定义动画: ```swift extension ViewController: UIViewControllerTransitioningDelegate { // 返回自定义的动画控制器对象 func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return BottomViewAnimator(isPresenting: true) } // 返回自定义的动画控制器对象 func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return BottomViewAnimator(isPresenting: false) } } ``` 在自定义动画控制器中,可以实现底部弹出视图的动画效果。可以通过使用 UIView 的动画方法来实现位移、透明度等动画效果。 最后,需要创建一个自定义的 Animator 类,实现 UIViewControllerAnimatedTransitioning 协议的相关代理方法来控制动画的展示和隐藏。 通过上述步骤,即可完成iOS底部弹出视图的实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值