spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

[spinner startAnimating];

[spinner setUserInteractionEnabled:NO]; // setting UserInteractionEnable to NO, any touch events are forwarded to the superview (the button)

spinner.center = CGPointMake(20, 18);

[self addSubview:spinner];

原文:地址:https://coderwall.com/p/4fydiq/uiactivityindicatorview-over-an-uibutton

Did you know that putting an UIActivityIndicatorView over a UIButton, all touch events are blocked by the spinner?

If you do the same with a UILabel or UIImageView, all events are forwarded to the button (its superview), like I (and you) expect. But with UIActivityIndicatorView this is not the behaviour you get. All these classes are extensions of UIView, so why they work differently?

The key difference is that the UIActivityIndicatorView is animated. When you have an animated view, iOS changes its userInteractionEnabled property to YES to prevent interactions (and interruptions) from the user with that view, during the animation phase.

How to fix that? Just set userInteractionEnabled to NO on the spinner, so all the touch events are forwarded again to the spinner superview (the button in my case) and everything works like I expect.

Here is the example code

spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner startAnimating];
[spinner setUserInteractionEnabled:NO]; // setting UserInteractionEnable to NO, any touch events are forwarded to the superview (the button)
spinner.center = CGPointMake(20, 18);
[self addSubview:spinner];

UIActivityIndicatorView添加到UIButton上并响应事件的更多相关文章

  1. 「iOS造轮子」之UIButton 用Block响应事件

    俗语说 一个不懒的程序员不是好程序员 造轮子,也只是为了以后更好的coding. coding,简易明了的代码更是所有程序员都希望看到的 无论是看自己的代码,还是接手别人的代码 都希望一看都知道这代码 ...

  2. UIImageView中的UIButton不响应事件解决方案

    如下: CGRect imageRect = (CGRect){, , , }; UIImageView *imageView = [[[UIImageView alloc] initWithFram ...

  3. 解决UIButton 连续点击重复响应事件问题

    经常会遇到重复点击某个按钮 事件被响应多次的情景, 有时候可能对程序本身并没有什么影响 , 可有时候偏偏需要限制button响应事件直接的间隔 . 方法一 : 标记 1 . 利用空闲enable属性来 ...

  4. 从uibutton的点击谈谈ios的响应事件

    最近在做一个项目,接连遇到两个关于点击事件的问题. 1.点击button不能响应事件的. 2.子view的frame超出了父view的容器大小,也不能响应点击事件. 效果图如右: 1.第一张图中的弹出 ...

  5. 文字添加响应事件,js动态加载CSS, js弹出DIV

    文字添加响应事件,js动态加载CSS, js弹出DIV <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  6. html .css 实现图片滑动和自动播放特效移动端 HTML 5中添加了以touch 开头的事件

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8">     <meta ...

  7. JVM上的响应式流 — Reactor简介

    强烈建议先阅读下JVM平台上的响应式流(Reactive Streams)规范,如果没读过的话. 官方文档:https://projectreactor.io/. 响应式编程 作为响应式编程方向上的第 ...

  8. JVM平台上的响应式流(Reactive Streams)规范

    // Reactive Streams // 响应式流是一个倡议,用来为具有非阻塞后压的异步流处理提供一个标准.大家努力的目标集中在运行时环境(JVM和JavaScript)和网络协议上. 注:响应式 ...

  9. 屏蔽响应事件继续向父视图传递的category

    屏蔽响应事件继续向父视图传递的category 这篇教程是上一篇教程的升级版,将复杂的代码封装成了category,更便于使用:) 效果: 源码: UIGestureRecognizer+EnvetI ...

随机推荐

  1. Codeforces Round #280 (Div. 2) A B C 暴力 水 贪心

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. mac下使用Solarized配色方案

    Solarized配色方案不用多介绍了.具体点击这里:http://ethanschoonover.com/solarized 我们首先搞定macvim 你需要下载solarized.vim配色文件, ...

  3. poj1733 带权并查集

    题意:有一个 0/1 数列,现在有n组询问和回答,表示某个区间内有奇数或者偶数个1,问到前多少个都没有逻辑错误,而下一个就不满足 可以定奇数为 1 偶数为 0作为每个元素的权值,表示它与它的祖先元素的 ...

  4. Scrum 项目2.0

    阅读教材第8章,8.1~8.3节 P157~168,了解获取用户需求的办法,每个组可以选择一二加以应用. 8.4节P168-171 查阅NABCDA模型的具体说明. 2.SCRUM 流程的步骤 1 完 ...

  5. 防止 SQL 注入的方法(摘抄)

    ——选自<深入Ajax : 架构与最佳实践 = Advanced Ajax : architecture and best practices/ (美)Shawn M.Lauriat著:张过,宋 ...

  6. Unity坐标系

    Unity 使用的是左手坐标系

  7. easyUI之datebox

    datebox具有输入的文本框及点击出现日历面板选择器的功能. 此组件的加载方式有class及jq方式,加载后,进行简单的属性设置即可. 对于此属性有一个很好的用法shareCalendar,就是多输 ...

  8. gitignore 忽略文件夹

    上面问题,按理说只要在.gitignore文件中加上 /dockAni/Library/ 即可实现. 但是我这样做了以后却发现sourceTree中仍然会列出Library中的文件. 最后在这个帖子中 ...

  9. unity, Animation crossfade需要两动画在时间上确实有交叠

    unity现在播动画都用Animator了,但公司的老项用的还是Animation,今天遇到一个bug,是两个动画的衔接处不连贯. 最后发现是由于A动画已经播完之后B动画才开始播,而且还用了cross ...

  10. unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里

    如果像下面这样写: using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Gene ...