美文网首页iOS&Xcode奇技淫巧喜欢的文章程序员
(一)自己动手写代码之PZLoadingButton

(一)自己动手写代码之PZLoadingButton

作者: ParkinWu | 来源:发表于2015-04-14 18:24 被阅读1258次

实现一个简单的button 先给出github链接

效果:
效果图效果图
需求:

点击,隐藏button上文字,将显示一个菊花,表示正在处理网络请求或者其他任务
再次点击,显示button文字,菊花停止转动,恢复最初状态

主要一下几个步骤:
  • 创建一个button的子类
  • 给button添加一个UIActivityIndicatorView(俗称菊花)
  • 在特定的时机,开启关闭旋转

代码:

创建button子类,PZLoadingButton
#import <UIKit/UIKit.h>
@interface PZLoadingButton : UIButton
- (void)startLoading;
- (void)stopLoading;
@end

需要两个接口,一个开始loading,一个停止loading

具体实现如下:
@property (nonatomic, strong) UIActivityIndicatorView * indecator;
- (void)startLoading {
    self.titleLabel.alpha = 0;
    [self.indecator startAnimating];
}
- (void)stopLoading {
    self.titleLabel.alpha = 1;
    [self.indecator stopAnimating];
}

那我们在button创建的时候将UIActivityIndicatorView添加到button上,添加button要考虑以下情况:

  1. 使用xib或storyboard + autolayout
    xib和 autolayout拖控件方式会调用- (id)initWithCoder:(NSCoder *)aDecoder方法
  2. 使用代码 + frame
    纯代码使用便利构造器+ (id)buttonWithType:(UIButtonType)buttonType会调用- (instancetype)initWithFrame:(CGRect)frame方法

分别对应以下两种初始化方法:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commentInit];
[self setupConstrains];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commentInit];
[self setupConstrains];

    }
    return self;
}

因为创建的菊花默认状态下是显示的,- (void)commentInit方法主要功能就是将其隐藏。
- (void)commentInit {
self.indecator.hidden = YES;
self.indecator.userInteractionEnabled = NO;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
}

- (void)setupConstrains {
    //X方向居中
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.indecator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    //Y方向居中
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.indecator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    //宽25
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.indecator attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:25]];
    //高25
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.indecator attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:25]];
}

到此为止,详细功能见代码

相关文章

  • (一)自己动手写代码之PZLoadingButton

    实现一个简单的button 先给出github链接 效果: 需求: 点击,隐藏button上文字,将显示一个菊花,...

  • 手写链表

    java基础之简单手写链表,来人,上代码:

  • 程序员面试时手写代码的意义?

    一.手写代码和上机测试 我觉得,手写要写出思路,上机要能运行就够了 写代码 二.手写代码的意义和作用 手写代码是一...

  • (三) 自己动手写代码之PZMovableFlowButton

    需求 实现iPhone上的AssistiveTouch一样的自动吸附效果。 效果图 (图看不到可以点这里)我外链个...

  • (二)自己动手写代码之PZSlideMenu

    简述 最近项目中用到一个抽屉效果,尝试自己写了一个,将过程记录下来, 有新需求改起来非常方便。我先实现最基本的功能...

  • 手写ArrayList

    java基础之简单手写ArrayList,闲话休提,直接上代码:

  • drupal8 配置三级联动,多级联动

    自己代码,手写集成 http://tshi0912.github.io/city-picker/ 自己配,不写代码...

  • 程序员编程10大原则

    ​ 1.想清楚,再动手写代码 刚入行的新手,为了展示自己的能力,拿到需求迫不及待地就开始上手写代码,大忌!!!!!...

  • 手写代码

    手写事件侦听器,并要求兼容浏览器 手写事件模型 手写事件代理,并要求兼容浏览器 手写事件触发器,并要求兼容浏览器 ...

  • 手写代码

    css 水平、垂直居中 1、 已知元素宽高<1>absolute+负margin --- 必须要定宽高<2>ab...

网友评论

    本文标题:(一)自己动手写代码之PZLoadingButton

    本文链接:https://www.haomeiwen.com/subject/tdvdfttx.html