不明觉厉iOSiOS学习傲视苍穹iOS《Objective-C》VIP专题

模仿QQ好友动态中的一个弹窗

2016-09-02  本文已影响393人  gitKong

需求

昨天接到一个需求,就是类似QQ好友动态,点击箭头的那个弹窗,如下图 第一张是QQ的,第二张是我模仿的


QQ好友动态弹窗.jpg
Custom Alert.gif

分析

实现思路

   @interface FLAlertViewHud : NSObject
@interface FLAlertView : UIView

FLAlertViewHud->FLAlertView->UITableView->UITableViewCell


#import <UIKit/UIKit.h>
// 修改弹窗的高度,内部自适应
#define FLTableViewCellHeight 44
typedef enum{
    FLAlertViewHudCellTypeShare,
    FLAlertViewHudCellTypeNormal
}FLAlertViewHudCellType;

// image name key
extern NSString * const FLAlertViewHudLeftImageKey;
// title text key
extern NSString * const FLAlertViewHudTitleLabelKey;
// cell type default is FLAlertViewHudCellTypeNormal
extern NSString * const FLAlertViewHudCellTypeKey;

@interface FLAlertViewHud : NSObject
/**
 *  @author 孔凡列, 16-09-03 05:09:32
 *
 *  单例
 *
 *  @return return value description
 */
+ (instancetype)shareAlertViewHud;
/**
 *  @author 孔凡列, 16-09-03 01:09:47
 *
 *  显示在指定位置
 *
 *  @param y 指定的y值
 */
- (void)fl_show:(CGFloat)y;
/**
 *  @author 孔凡列, 16-09-03 06:09:38
 *
 *  显示在指定位置
 *
 *  @param y   指定的y值
 *  @param arr 字典数组(指定key的字典)
 */
- (void)fl_show:(CGFloat)y arr:(NSArray<NSDictionary *> *)arr;

/**
 *  @author 孔凡列, 16-09-03 01:09:16
 *
 *  根据弹窗的高度自适应弹窗的显示位置,在view的上面还是下面,目前只有两种情况
 *
 *  @param view view description
 */
- (void)fl_showAround:(UIView *)view;
/**
 *  @author 孔凡列, 16-09-03 06:09:55
 *
 *  根据弹窗的高度自适应弹窗的显示位置,在view的上面还是下面,目前只有两种情况
 *
 *  @param view 指定的view
 *  @param arr  字典数组(指定key的字典)
 */
- (void)fl_showAround:(UIView *)view arr:(NSArray<NSDictionary *> *)arr;
/**
 *  @author 孔凡列, 16-09-03 06:09:16
 *
 *  消失弹窗
 */
- (void)fl_dismiss;

/**
 *  @author 孔凡列, 16-09-03 01:09:05
 *
 *  分享按钮的点击事件---适应项目需求(可再次封装)
 */
@property (nonatomic,copy)void(^fl_shareCellOperationBlock)(NSInteger index);
/**
 *  @author 孔凡列, 16-09-03 01:09:52
 *
 *  普通cell的点击事件
 */
@property (nonatomic,copy)void(^fl_normalCellOperationBlock)(NSInteger row);

@end
显示调用如下:
    NSArray *arr = @[
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_4",FLAlertViewHudTitleLabelKey : @"分享",FLAlertViewHudCellTypeKey : @(FLAlertViewHudCellTypeShare)},
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_5",FLAlertViewHudTitleLabelKey : @"收藏"},
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_6",FLAlertViewHudTitleLabelKey : @"转载照片"},
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_7",FLAlertViewHudTitleLabelKey : @"隐藏此条动态"},
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_8",FLAlertViewHudTitleLabelKey : @"不看他的动态"},
                     @{FLAlertViewHudLeftImageKey : @"Snip20160903_9",FLAlertViewHudTitleLabelKey : @"收藏"}
                     ];
    
    [[FLAlertViewHud shareAlertViewHud] fl_showAround:btn
                                                  arr:arr];
事件处理如下:
   [FLAlertViewHud shareAlertViewHud].fl_normalCellOperationBlock = ^(NSInteger row){
        NSDictionary *dict = arr[row];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dict[FLAlertViewHudTitleLabelKey] message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    };
    
    [FLAlertViewHud shareAlertViewHud].fl_shareCellOperationBlock = ^(NSInteger index){
        NSLog(@"在 %zd 行 点击%zd按钮",indexPath.row,index);
        NSString *str = nil;
        switch (index) {
            case 0:
                str = @"QQ分享";
                break;
            case 1:
                str = @"微信分享";
                break;
            default:
                str = @"朋友圈分享";
                break;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    };
面向模型封装后:

    NSArray *arr = @[
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"分享" leftImageName:@"Snip20160903_4" alertViewCellType:FLAlertViewHudCellTypeShare],
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"收藏" leftImageName:@"Snip20160903_5"],
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"转载照片" leftImageName:@"Snip20160903_6"],
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"隐藏此条动态" leftImageName:@"Snip20160903_7"],
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"不看他的动态" leftImageName:@"Snip20160903_8"],
                     [FLAlertViewModel fl_alertViewModelWithTitleName:@"收藏" leftImageName:@"Snip20160903_9"]
                     ];
    
    [[FLAlertViewHud shareAlertViewHud] fl_showAround:btn
                                                  arr:arr];

m文件代码太多了,这里就不拷贝过来了。。。。两种模式任君选择


总结

更新啦~~~面向模型也在上面喔


上一篇 下一篇

猜你喜欢

热点阅读