我想实现一个类似于滑块的功能。例如,按钮跟随手指从左向右移动。\n如下所示:\n
我应该使用 UISwipeGestureRecognizer。或者 UIPanGestureRecognizer\xef\xbc\x9f\n我使用了滑动手势但我不知道如何更新框架。
\n\nvar swipeRight = UISwipeGestureRecognizer(target: self, action: \n#selector(self.respondToSwipeGesture(sender:)))\nswipeView.isUserInteractionEnabled = true\nswipeView.addGestureRecognizer(swipeRight)\nRun Code Online (Sandbox Code Playgroud)\n 我花了很长时间尝试堆叠以编程方式创建的视图。我以编程方式查看了在 UIStackView 中添加视图的示例,但这不起作用。下面列出的是代码,我setUpListings从视图控制器调用。有两个条目,但仅显示一个条目。
import UIKit
import SnapKit
class ListingsView : UIView {
var containerView: UIView!
var listingsContainerView: UIStackView!
init() {
super.init(frame: CGRect.zero)
setUpContainerView()
setUpListingsContainer()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setUpContainerView() {
containerView = UIView()
self.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.height.equalTo(self)
make.width.equalTo(self)
containerView.backgroundColor = UIColor.white
}
}
func setUpListingsContainer() {
listingsContainerView = UIStackView()
listingsContainerView.distribution = .equalSpacing
listingsContainerView.alignment = .fill
listingsContainerView.axis = .vertical
listingsContainerView.spacing = 10
listingsContainerView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(listingsContainerView)
listingsContainerView.snp.makeConstraints { (make) …Run Code Online (Sandbox Code Playgroud) 我尝试使用 lineDashPattern 创建一个仅在角落有边框(qrCode 样式)的视图,但线之间的空间似乎与我定义的模式不匹配。这是我的游乐场:
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let myView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 200))
myView.backgroundColor = .black
let borderLayer = CAShapeLayer()
let d = (myView.bounds.width / 2) as NSNumber
borderLayer.strokeColor = UIColor.blue.cgColor
borderLayer.lineDashPattern = [d]
borderLayer.frame = myView.bounds
borderLayer.fillColor = nil
borderLayer.lineWidth = 10
borderLayer.strokeStart = 0.1
//borderLayer.lineDashPhase = -80
borderLayer.path = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 30, height: 30)).cgPath
borderLayer.lineCap = CAShapeLayerLineCap.round
myView.layer.addSublayer(borderLayer)
view.addSubview(myView)
self.view …Run Code Online (Sandbox Code Playgroud) 我很难理解convert(_:to:)和之间的区别convert(_:from:)由于某种原因,
假设我有两种观点(superView和subview)和一个根视图:
let superView = UIView(frame: CGRect(origin: .init(x: 25, y: 25), size: CGSize(width: 100, height: 100)))\nrootView.addSubview(view)\n\nlet subview = UIView(frame: CGRect(origin: .init(x: 25, y: 25), size: .init(width: 50, height: 50)))\nsuperView.addSubview(subview)\nRun Code Online (Sandbox Code Playgroud)\nconvert(_:to:)根据文档,将接收器\xe2\x80\x99s 坐标系中的点转换为指定视图的点:
let convertToWindow = subview.convert(subview.bounds, to: window)\n// {x 50 y 50 w 50 h 50 }\nRun Code Online (Sandbox Code Playgroud)\nconvert(_:from:)根据文档,将给定视图的坐标系中的点转换为接收器的坐标系:
let convertFromWindow = subview.convert(subview.bounds, from: window)\n// {x -50 y -50 w 50 h …Run Code Online (Sandbox Code Playgroud) 嘿,
我正在尝试通过更改拇指图像并在图片上添加标签来自定义滑块。为此,在我的视图中,我设置了滑块拇指的图像:
class SliderView: NibLoadingView {
@IBOutlet weak var slider: ThumbTextSlider!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
contentView = legacyLoadXib()
setup()
}
override func setup() {
super.setup()
self.slider.setThumbImage(UIImage(named: "onb_cc_slider_thumb")!, for: .normal)
self.slider.thumbTextLabel.font = UIFont(name: Fonts.SanFranciscoDisplayRegular, size: 14)
}
}
Run Code Online (Sandbox Code Playgroud)
在 ThumbTextSlider 类中,我设置文本标签如下:
class ThumbTextSlider: UISlider {
var thumbTextLabel: UILabel = UILabel()
private var thumbFrame: CGRect {
return thumbRect(forBounds: bounds, trackRect: trackRect(forBounds: bounds), value: value)
}
override func layoutSubviews() {
super.layoutSubviews()
thumbTextLabel.frame = thumbFrame
}
override func awakeFromNib() {
super.awakeFromNib() …Run Code Online (Sandbox Code Playgroud) 这是一种将子视图固定到UIView实例的方法。请原谅凌乱的代码,我一直在尝试让它工作。
open static func withEmbeddedView(_ view: UIView, andFixedHeight height: CGFloat) -> PMGAlertControllerEmbedComponent {
let base = PMGAlertControllerEmbedComponent(frame: CGRect.zero)
base.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
base.translatesAutoresizingMaskIntoConstraints = false
let left = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: base, attribute: .left, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: base, attribute: .right, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: base, attribute: .top, multiplier: 1, constant: 0)
let bottom …Run Code Online (Sandbox Code Playgroud) 我有一个 UIView,它是我的 VC 的视图(self.view),其中我添加了另一个 UIView 作为子视图,它们看起来都像一个正方形。我想限制子视图在父视图中的移动。基本上子视图不应该超出超级视图的范围。
我确实实施了
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// i check if point of touch is inside my superview i set the center i.e
UITouch *touch = [[UITouch allObjects] firstObject];
if (CGRectContainsPoint(subview.frame, [touch locationInView:self.view])) {
// set the center here... i.e
subview.center = [touch locationInView:self.view];
}
Run Code Online (Sandbox Code Playgroud)
这有效并且子视图正在移动,但它也会移动到超视图之外;如何仅限制超级视图内的移动?
我什至尝试过类似的东西。
if (CGRectContainsRect(self.view.bounds, subview.frame)) {
// this condition works but i don't know why my subview sticks to the sides of superview if i try to drag it …Run Code Online (Sandbox Code Playgroud) 如何设置自定义视图MKAnnotationView?我希望我的地图图钉通过UIView. 该UIView子类中可能有我想要自定义的其他视图。
网上有很多关于如何设置注释图像的示例,但没有如何实际更改该注释:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if !(annotation is MKPointAnnotation) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView!.canShowCallout = true
}
else {
annotationView!.annotation = annotation
}
let pinImage = UIImage(named: "customPinImage")
annotationView!.image = pinImage
return annotationView
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义 UIView 并为其设置了一个协议。现在,当我将委托设置为 self 时,从视图控制器中我得到一个 EXC_BAD_ACCESS。
---视图控制器代码------
class VerificationController: UIViewController, LoadingViewDelegate {
@IBOutlet weak var instructionView: LoadingView!
override func viewDidLoad() {
super.viewDidLoad()
instructionView.randomTextIndexes = [1]
instructionView.delegate = self
}
...
}
// "instructionView" is the UIView outlet and "LoadingView" is the class
Run Code Online (Sandbox Code Playgroud)
--这是自定义查看代码 ------
protocol LoadingViewDelegate {
func generated(random code:String)
}
class LoadingView: UIView {
var delegate:LoadingViewDelegate?
var randomTextIndexes:[Int] = []
}
Run Code Online (Sandbox Code Playgroud)
EXC_BAD_ACCESS当我尝试访问委托以及randomTextIndexes来自viewDidLoad()视图控制器的方法时,我得到了。你能告诉我我在这里缺少什么吗?
我无法加载我的子视图。我将它们定义为弱WKWebKit插座。如果我这样做view = tickerView或view = graphView,它的工作原理,但它只加载其中之一。我想加载两者。我该怎么做?
override func loadView() {
tickerView = WKWebView()
tickerView.navigationDelegate = self
self.view.addSubview(tickerView)
graphView = WKWebView()
graphView.navigationDelegate = self
self.view.addSubview(graphView)
}
Run Code Online (Sandbox Code Playgroud) uiview ×10
swift ×9
ios ×8
objective-c ×2
cashapelayer ×1
cocoa-touch ×1
delegates ×1
mapkit ×1
slider ×1
snapkit ×1
uislider ×1
uitouch ×1
wkwebview ×1