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. SQLServer如何用T—SQL命令查询一个数据库中有哪些表

    1.查询SQL中的所有表: Select TABLE_NAME FROM 数据库名称.INFORMATION_SCHEMA.TABLES Where TABLE_TYPE='BASE TABLE' 执 ...

  2. linux脚本编程技术---8

    一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格式 #!表明该脚本的的解 ...

  3. php部分---include()与require()的区别、empty()与isset is_null的区别与用法详解

    include()与require()的用途是完全一样的,不一定非得哪个放在最前面哪个放在中间.他们最根本的区别在于错误处理的方式不一样. 1.处理错误的方式: require()一个文件存在错误的话 ...

  4. 水灾(sliker.cpp/c/pas) 1000MS 64MB

    大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图表示,地图上 ...

  5. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  6. knockoutjs foreach array绑定 表格 下拉框绑定

    转载http://desert3.iteye.com/blog/1480471 knockoutjs foreach array绑定 表格 下拉框绑定 博客分类: Javascript   动态表格使 ...

  7. Amazon-countDuplicate

    Print the count of duplicate char in a given string in same order. Ex: Input- 'abbaccdbac', Output- ...

  8. Cycles_per_instruction

    https://en.wikipedia.org/wiki/Cycles_per_instruction

  9. C# waitformultipleobjects()

    class WatchThread { [DllImport("kernel32.dll")] private static extern int CreateEvent(IntP ...

  10. Unity3D的杂记

    刷新帧的不同控制函数 FixedUpdate 可以多次调用: 不饿能用于帧频很高的情况: Update 仅一次调用(每帧): LateUpdate 每帧调用一次: Corountine 用startC ...