iOS-Vendor第三方库iOS界面特效篇

XAlert------ 封装的一个提醒框

2015-09-13  本文已影响1597人  StrongX

自己封装的一个提醒框,包括上拉菜单、警告款。
先上图:


10.gif

一共包括四种形态,

第一种:
最普通的上拉菜单,来看下代码:
<pre><code>
XActionSheet *sheet = [[XActionSheet alloc]initWithTitleAndDesc:@"这是一个上啦菜单" Desc:@"你可以在这里填写一些详细内容"];
[sheet addCancelButtonWithTitle:@"取消"];
[sheet addButtonwithTitle:@"按钮一"];
[sheet addButtonwithTitle:@"按钮二"];
[sheet addButtonwithTitle:@"按钮三"];
[sheet show];
</code></pre>

类名叫做XActionSheet 期中包括一个delegate;所有的点击事件都通过这个delegate来传递,
-(void)buttonClick:(NSInteger)index;
传递的参数是按钮的tag;

第二种:
带一个我现在看起来很奇葩的动画,来看代码:

<pre><code>
XActionSheet *sheet = [[XActionSheet alloc]initWithTitleAndDesc:@"这是一个上啦菜单" Desc:@"你可以在这里填写一些详细内容"];
[sheet addCancelButtonWithTitle:@"取消"];
[sheet addButtonwithTitle:@"按钮一"];
[sheet addButtonwithTitle:@"按钮二"];
[sheet addButtonwithTitle:@"按钮三"];
[sheet showInAnimate];
</code></pre>

和上面唯一的区别就是show的方法不一样,其他都一样。

XActionSheet这个类还有一些其他的属性我们来看一下

<pre><code>

@property (nonatomic, strong) UILabel *Title;
@property (nonatomic, strong) UILabel *Desc;
@property (nonatomic, strong) UIButton *CancelButton;
@property (nonatomic, strong) NSMutableArray *btnArray;

@property (nonatomic, assign) id<XActionSheetDelegate>delegate;
</code></pre>

看名字都明白是干嘛的啦,期中所有的按钮都放在了btnArray这个数组当中,你们可以使用这个数组来获取到button然后对button进行自定义。

我们再来看一下XAlerView

第一种从上面掉下来的这种动画:

<pre><code>
XAlertView *alert = [[XAlertView alloc]initWithTitleAndDesc:@"这是一个警告框" Desc:@"这里可以填一些详细内容"];
alert.btnTitleArray = @[@"按钮一",@"按钮二",@"按钮三"];
[alert showAnimation1];
</code></pre>

你没看错,一共就三行代码

再看第二种:

<pre><code>
XAlertView *alert = [[XAlertView alloc]init];
alert.containViewHeight = 100;
CGFloat width = alert.containView.frame.size.width;
UIImageView *avatar = [[UIImageView alloc]initWithFrame:CGRectMake((width - 60)/2, 10, 60, 60)];
avatar.layer.cornerRadius = 30;
avatar.layer.masksToBounds = true;
avatar.image = [UIImage imageNamed:@"cute_girl"];
[alert.containView addSubview:avatar];

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + 60 + 10,width, 20)];
name.text = @"StrongX";
name.textColor = [UIColor darkGrayColor];
name.textAlignment = NSTextAlignmentCenter;
[alert.containView addSubview:name];

alert.btnTitleArray = @[@"确定",@"关闭"];
alert.delegate = self;
[alert showAnimation2];

</code></pre>

这个和上面不一样 ,初始化方法就不一样,第一种初始化方法是通过title和desc的方法,会自动创建两个label,和默认的提示框长相类似,但是很多时候我们都希望都能来自定义内容,就像我的gif图中那样,放一个头像,你也可能是放一个textview······你可以通过设定alert.containViewHeight的值来改变 containView的高度,当然这个值是不包括下面的按钮的,所以整个View的高度就是alert.containViewHeight加上50(下面的按钮的高度是50).
同样的所有的按钮我都放在了btnArray这个数组里,你可以用来修改一些东西,点击事件是通过一个delegate来调用的

<pre><code>

@protocol XAlertDelegate <NSObject>

@optional
-(void)btnClicked:(NSInteger)index;

@end
</code></pre>

就一个方法,方法就一个参数,参数就是第几个按钮(按钮的tag)。

已经上传github了:https://github.com/StrongX/XAlert

上一篇 下一篇

猜你喜欢

热点阅读