简体   繁体   English

使UIView像UIButton一样工作

[英]Make UIView work like a UIButton

I built a Custom Cell in IB and in it created a view that represents a button. 我在IB中构建了一个Custom Cell ,并在其中创建了一个代表按钮的视图。 It has Labels and ImageViews in it. 它具有LabelsImageViews

I want to know if there is a way to make that view act like a button. 我想知道是否有一种方法可以使该视图像按钮一样。 That is, show a shadow and call a method when is pressed. 即,显示阴影并在按下时调用方法。

Yes. 是。 Just implement tableView:didSelectRowAtIndexPath: and you can do everything you could do in a button action @selector . 只需实现tableView:didSelectRowAtIndexPath:您就可以完成按钮操作@selector可以做的所有事情。

As for the shadow and the rounded rectangle button feel, if you use a single cell in a section with UITableViewStyleGrouped , it will be pretty close. 至于阴影和圆角矩形按钮的感觉,如果您在具有UITableViewStyleGrouped的节中使用单个单元格,它将非常接近。

The best is to make it a button. 最好是使其成为按钮。 You can use UIButtonTypeCustom and set the background to the cell you've created. 您可以使用UIButtonTypeCustom并将背景设置为您创建的单元格。

您可以使用UITapGestureRecognizer并通过委托传递事件,也可以使用UIControl(它是UIView的子类,但具有UIButton tap事件,如touchUpInside等),然后将事件与委托传递给视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM