1

我想创建一组按钮,它们的工作和感觉非常像Windows Phone 上可用的 Metro 风格磁贴。我想允许用户点击它们来访问他们的底层功能(打开一个模式或其他东西或那种)。

  1. 我担心子类化 UIButton 不会让我到达那里。我需要瓷砖本身内部的方角和动态内容。我在 SO 周围发现的帖子似乎表明子类化 UIButton 不是一个好主意,原因有几个,我应该改用 UIView。该回复来自 2010,我不知道在此后的 2 年多时间里,如何实现这一效果发生了相当大的变化。现在大多数用户都将拥有 iOS 5.1+,如果不是 6.0 的话。

  2. 假设我确实走 UIView 路线。我应该实现 UIResponder 的触摸事件还是应该转而使用 UITapGestureRecognizer 路由?2012年更好的做法是什么?

谢谢!

4

3 回答 3

2

I'd suggest subclassing from UIControl instead of UIButton. I've seen how those windows tiles look and work in some videos including flipping animations when content changes etc., and it would be easier to package that functionality into a Tile class or something similar than having to carry a modified UIButton everywhere, and doing it for every such instance you need to create.

The reason I'm suggesting to not subclass from UIButton is because it provides support for 1 image and 1 label. You seem to have multiple images and labels on all tiles, so you'll have to start managing your own subviews. Instead of working around the 1 label and image you get in a UIButton, it's cleaner to start from scratch with a UIControl and create your tiles.

于 2012-09-18T03:27:29.993 回答
0

如果您只是希望拥有几乎像常规按钮一样的方形 Metro 风格按钮(按下它们会发生一些事情),那么您可以使用自定义按钮从标准 UIButton 类中获取所有功能,而无需子类化。

这是一个简单的例子,在代码中,但这也可以在 IB 中完成。假设您有一个名为:tile1.png

//set the image
UIImage *image = [UIImage imageNamed:@"tile1.jpg"];

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//add the image
[button setImage:image forState:UIControlStateNormal];

//define the action
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

如果需要修改框架大小可以通过 button.frame 也可以修改一些其他的按钮属性。

您是否知道所有这些并且正在寻找其他东西或者这有帮助吗?好起来

于 2012-09-18T02:59:40.793 回答
0

A use case would be to use a custom-looking button within Storyboard / Interface Builder. Having a subclass allows you to directly use this custom-looking button from Interface Builder.

于 2016-01-31T11:13:18.900 回答