[英]How do you add touch outlets to custom UIView?
I've created the following custom UIVIew: 我创建了以下自定义UIVIew:
//
// YearSelectorSegControl.swift
// OurDailyStrength iOS
//
// Created by Rob Avery on 8/28/17.
// Copyright © 2017 Rob Avery. All rights reserved.
//
import UIKit
@IBDesignable
class YearSelectorSegControl: UIView {
var buttons = [UIButton]()
var selector: UIView!
var sv: UIStackView!
var currentPosition: Int = 0
@IBInspectable
var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable
var textColor: UIColor = .lightGray {
didSet {
updateView()
}
}
@IBInspectable
var textBackground: UIColor = .clear {
didSet {
updateView()
}
}
func updateView() {
// ... code here ...
}
override func draw(_ rect: CGRect) {
layer.cornerRadius = 0
}
}
When I go to the Interface Builder, and try and connect this UIView to the controller, the only thing allowed is an outlet. 当我转到界面生成器并尝试将此UIView连接到控制器时,唯一允许的是插座。 I want to be able to connect this custom UIView to a function when it's touched.
我希望能够在触摸此自定义UIView并将其连接到函数。 How do I do that?
我怎么做?
You have to subclass UIControl instead of UIView 您必须继承UIControl而不是UIView
@IBDesignable
class YearSelectorSegControl: UIControl {
...
}
Using storyboard, 使用情节提要,
Change the class of UIView
on identity inspector as UIButton
将身份检查器上的
UIView
的类更改为UIButton
Now you can set action connection to that UIView
as UIButton
现在您可以将与该
UIView
动作连接设置为UIButton
See below code 见下面的代码
@IBAction func clickView(_ sender: UIButton) {
//perform your actions in here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.