Swift views and controls

A view displays content, whereas a control is used to modify it in some way. A control (UIControl) is a subclass of UIView. views can not have action. But Gesture recognizers can be used to add actions for the views.

An image view isn’t a control, so it’s not designed to respond to input in the same way that a control—such as a button—responds. For example, you can’t simply create an action method that’s triggered when a user taps on an image view. (If you try to Control-drag from the image view to your code, you’ll notice that you can’t select Action in the Connection field.)

Fortunately, it’s quite easy to give a view the same capabilities as a control by adding a gesture recognizer to it. Gesture recognizers are objects that you attach to a view that allow the view to respond to actions the way a control does. Gesture recognizers interpret touches to determine whether they correspond to a specific gesture, such as a swipe, pinch, or rotation. You can write an action method that occurs when a gesture recognizer recognizes its assigned gesture, which is exactly what you need to do for the image view.

Attach a tap gesture recognizer (UITapGestureRecognizer) to the image view, which will recognize when a user has tapped the image view. You can do this easily in your storyboard.

To add a tap gesture recognizer to your image view

  1. Open the Object library. (To open it quickly, choose View > Utilities > Show Object Library.)
  2. In the Object library, type tap gesture in the filter field to find the Tap Gesture Recognizer object quickly.
  3. Drag a Tap Gesture Recognizer object from the Object library to your scene, and place it on top of the image view
时间: 2024-08-08 15:23:09

Swift views and controls的相关文章

Awesome Swift

Awesome Swift A collaborative list of awesome Swift resources,inspired by awesome-python and listed on awesome-awesomeness. Feel free to contribute! Awesome Swift Demo Apps iOS Apple Watch OS X Dependency Managers Guides Editor Support Vim Libs Anima

swift纯代码 demo

class serviceHallViewController: UIViewController  ,UITableViewDelegate ,UITableViewDataSource {          var tableView :UITableView?          override func viewDidLoad() {     super.viewDidLoad()     //self.title = "MySwift"     setupViews()   

iOS Tableview侧滑删除和移动cell的实现

慕课网上学习了tableview的使用,突然让我觉得iOS比android简单多了,可能是我的感觉吧.因为android实现list view侧拉删除,动态移动item过程还是稍微有点复杂的.但是iOS却只需要重写几个方法就可以实现了.我只能说iOS太神奇!我就跟着做了一下. 项目地址:Todo 看效果,UI还可以.先上storyboard结构图: navigate controller 实现一个导航栏.view controller 实现一个tableview,tableviewCell .

IOS总结(学习过程中整理的笔记)

MVC模式:(model+view+controller):是一种帮你把代码功能和显示划分出来的设计模式: model:较为底层的数据引擎,负责管理实体中所继承的数据: view:和用户交互界面: controller:连接二者的桥梁: cocoa frameworks 有两个框架: foundation foundation  是cocoa中最基本的一些类:再mac应用程序中负责对象管理,内存管理,容器等相关数据: uikit: uikit:为程序提供可视化的底层构架,包括窗口,视图,控件类和

UITableView的创建及其一些常用方法

UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate. 一.UITableView的创建 1.代码方式: 1 UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 2 tableView.backgroundColor=[UIColor grayColor]; 3 [self.view addSu

The App Life Cycle & The Main Function

The App Life Cycle Apps are a sophisticated interplay between your custom code and the system frameworks. The system frameworks provide the basic infrastructure that all apps need to run, and you provide the code required to customize that infrastruc

自定义一个"花瓣"菜单-b

先来看一下效果 XLCircleMenu.gif 是不是觉得挺好玩的呀. 通过这篇文章你可以学到: 1.系统UITableView的部分设计思想 2.自定义控件常用设计思路 3.动画的具体使用 4.手势的具体使用 4.装逼一点,良好的代码风格 5...... 开始码 随机颜色为了快速区分视图,这里用了随机颜色来区分,生成随机颜色的方式比较多.常见的获取方法为宏如下: #define RandomColor [UIColor colorWithRed:arc4random_uniform(255)

关于iOS的一些总结

1??字典转模型 一)有一个模型的情况 1.看plist文件的根节点是否是NSArray类型的: 2.如果是:根据文件中的字典元素,创建一个对应的模型类,类中的属性就是字典中每一个key值,类型是键值对应的类型(模型类的属性一定要跟字典中的key值一致,否则使用KVC会出错). 3.自定义一个类的init方法,该方法是对象方法, eg:- (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [s

12-26 tableView的学习心得

一:基础部分 UITableView的两种样式: 注意是只读的 1.UITableViewStytlePlain(不分组的) n 2.UITableViewStyleGrouped(分组的) 二:如何展示数据 1. (1)UITableView需要一个数据源(dataSource)来显示数据: 需要注意dataSource是UITableView的一个属性,类型是id(任意形),但是需要遵守<UITableViewDataSource>协议! (2)UITableView会向数据源查询一共多少