iPhone版的弹出视图

iPhone版的弹出视图


使用方法:
* 实例化
* 回调方法
* show方法

#import "PopoverView.h"
PopoverView *pop;//弹出pop视图

- (void)createPopView{
    @weakify(self);
    if (CurrentDevice) {
        //选项图标
        UIImage *moreImg = [UIImage imageNamed:@"Main_More"];
        //初始化方法
        pop = [[PopoverView alloc] initWithPoint:CGPointMake(naviView.moreBtn.center.x, STATUSBARHEIGHT+(NAVIBARHEIGHT-moreImg.size.height)/2+moreImg.size.height+6*BasicHeight) titles:sections images:nil];
        __weak DeviceModel *model =CurrentDevice;
        __weak NSString *devicename = deviceName;
        //回调方法
        pop.selectRowAtIndex = ^(NSInteger index){
            @strongify(self);
            if (index == 0) {
                [self bindDeviceAction];
            }else if (index == 1) {
                if (model) {
                    EditDeviceNameViewController *deviceNameVC = [[EditDeviceNameViewController alloc] init];
                    deviceNameVC.deviceInfo = model;
                    deviceNameVC.name = devicename;
                    [self pushAndRemoveGuesture:deviceNameVC];
                }
                return ;
            }

            }
        };
        //show方法
        [pop show];
    }
    }

}

代码实例

//
//  PopoverView.h
//  ArrowView
//
//  Created by guojiang on 4/9/14.
//  Copyright (c) 2014年 LINAICAI. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import <UIKit/UIKit.h>

interface PopoverView : UIView

-(id)initWithPoint:(CGPoint)point titles:(NSArray *)titles images:(NSArray *)images;
-(void)show;
-(void)dismiss;
-(void)dismiss:(BOOL)animated;

property (nonatomic, copy) UIColor *borderColor;
@property (nonatomic, copy) void (^selectRowAtIndex)(NSInteger index);

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@end

------
#import "PopoverView.h"

#define kArrowHeight 10.f
#define kArrowCurvature 6.f
#define SPACE 2.f
#define ROW_HEIGHT 44.f
#define TITLE_FONT [UIFont systemFontOfSize:16]
#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

@interface PopoverView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic) CGPoint showPoint;

@property (nonatomic, strong) UIButton *handerView;

@end

@implementation PopoverView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.borderColor = RGB(200, 199, 204);
        self.backgroundColor = [UIColor clearColor];

    }
    return self;
}

-(id)initWithPoint:(CGPoint)point titles:(NSArray *)titles images:(NSArray *)images
{
    self = [super init];
    if (self) {
        self.showPoint = point;
        self.titleArray = titles;
        self.imageArray = images;
        //获取/设置view的frame
        self.frame = [self getViewFrame];

        [self addSubview:self.tableView];

    }
    return self;
}

-(CGRect)getViewFrame
{
    CGRect frame = CGRectZero;

    frame.size.height = [self.titleArray count] * ROW_HEIGHT + SPACE + kArrowHeight;

    for (NSString *title in self.titleArray) {
        CGFloat width =  [UIConfig sizeWithString:title Font:TITLE_FONT maxSize:CGSizeMake(300, 100)].width;
        frame.size.width = MAX(width, frame.size.width);
    }

    if ([self.titleArray count] == [self.imageArray count]) {
        frame.size.width = 10 + 25+10+ frame.size.width + 40;
    }else{
        frame.size.width = 10 + frame.size.width + 50;
    }

    frame.origin.x = self.showPoint.x - frame.size.width/2;
    frame.origin.y = self.showPoint.y;

    //左间隔最小5x
    if (frame.origin.x < 5) {
        frame.origin.x = 5;
    }
    //右间隔最小5x
    if ((frame.origin.x + frame.size.width) > ScreenWidth-5) {
        frame.origin.x = ScreenWidth-5 - frame.size.width;
    }

    return frame;
}

-(void)show
{
    self.handerView = [UIButton buttonWithType:UIButtonTypeCustom];
    [_handerView setFrame:[UIScreen mainScreen].bounds];
    [_handerView setBackgroundColor:[UIColor clearColor]];
    [_handerView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [_handerView addSubview:self];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    [window addSubview:_handerView];

    CGPoint arrowPoint = [self convertPoint:self.showPoint fromView:_handerView];
    self.layer.anchorPoint = CGPointMake(arrowPoint.x / self.frame.size.width, arrowPoint.y / self.frame.size.height);
    self.frame = [self getViewFrame];

    self.alpha = 0.f;
    self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
    [UIView animateWithDuration:0.2f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
        self.alpha = 1.f;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.08f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.transform = CGAffineTransformIdentity;
        } completion:nil];
    }];
}

-(void)dismiss
{
    [self dismiss:YES];
}

-(void)dismiss:(BOOL)animate
{
    if (!animate) {
        [_handerView removeFromSuperview];
        return;
    }

    [UIView animateWithDuration:0.3f animations:^{
        self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
        self.alpha = 0.f;
    } completion:^(BOOL finished) {
        [_handerView removeFromSuperview];
    }];

}

#pragma mark - UITableView

-(UITableView *)tableView
{
    if (_tableView != nil) {
        return _tableView;
    }

    CGRect rect = self.frame;
    rect.origin.x = SPACE;
    rect.origin.y = kArrowHeight + SPACE;
    rect.size.width -= SPACE * 2;
    rect.size.height -= (SPACE - kArrowHeight);

    self.tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.alwaysBounceHorizontal = NO;
    _tableView.alwaysBounceVertical = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.scrollEnabled = NO;
    _tableView.backgroundColor = [UIColor clearColor];
//    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    return _tableView;
}

#pragma mark - UITableView DataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titleArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.backgroundView = [[UIView alloc] init];
    cell.backgroundView.backgroundColor = RGB(245, 245, 245);

    if ([_imageArray count] == [_titleArray count]) {
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    if(self.titleArray.count >1)
    {
        if (indexPath.row==0) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"303030"];
        }
       else if (indexPath.row ==1) {
            NSString * firstTitle =[_titleArray objectAtIndex:0];

            if (firstTitle.length>6) {
                NSMutableAttributedString *attribute =[[NSMutableAttributedString alloc]initWithString:firstTitle];
                [attribute addAttribute:NSForegroundColorAttributeName value:[UIConfig colorFromHexRGB:@"303030"] range:NSMakeRange(0, 6)];
                 [attribute addAttribute:NSForegroundColorAttributeName value:[UIConfig colorFromHexRGB:@"585858"] range:NSMakeRange(6, firstTitle.length-6)];
                cell.textLabel.attributedText = attribute;
            }else{
                cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
                cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"303030"];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //    cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"585858"];
        }else if (indexPath.row==2) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"f96469"];

        }else if (indexPath.row==3){
            cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"303030"];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }else{
        cell.textLabel.textColor = [UIConfig colorFromHexRGB:@"303030"];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark - UITableView Delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.selectRowAtIndex) {
        self.selectRowAtIndex(indexPath.row);
    }
    [self dismiss:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ROW_HEIGHT;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self.borderColor set]; //设置线条颜色

    CGRect frame = CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height - kArrowHeight);

    float xMin = CGRectGetMinX(frame);
    float yMin = CGRectGetMinY(frame);

    float xMax = CGRectGetMaxX(frame);
    float yMax = CGRectGetMaxY(frame);

    CGPoint arrowPoint = [self convertPoint:self.showPoint fromView:_handerView];

    UIBezierPath *popoverPath = [UIBezierPath bezierPath];
    [popoverPath moveToPoint:CGPointMake(xMin, yMin)];//左上角

    /********************向上的箭头**********************/
    [popoverPath addLineToPoint:CGPointMake(arrowPoint.x - kArrowHeight, yMin)];//left side
    [popoverPath addCurveToPoint:arrowPoint
                   controlPoint1:CGPointMake(arrowPoint.x - kArrowHeight + kArrowCurvature, yMin)
                   controlPoint2:arrowPoint];//actual arrow point

    [popoverPath addCurveToPoint:CGPointMake(arrowPoint.x + kArrowHeight, yMin)
                   controlPoint1:arrowPoint
                   controlPoint2:CGPointMake(arrowPoint.x + kArrowHeight - kArrowCurvature, yMin)];//right side
    /********************向上的箭头**********************/


    [popoverPath addLineToPoint:CGPointMake(xMax, yMin)];//右上角

    [popoverPath addLineToPoint:CGPointMake(xMax, yMax)];//右下角

    [popoverPath addLineToPoint:CGPointMake(xMin, yMax)];//左下角

    //填充颜色
    [RGB(245, 245, 245) setFill];
    [popoverPath fill];

    [popoverPath closePath];
    [popoverPath stroke];
}

@end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值