iPhone / iOS自定义控件 [英] iPhone / iOS custom control

查看:103
本文介绍了iPhone / iOS自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从头开始创建自定义iPhone控件,或者使用现有的库或框架。

I would like to know how to create a custom iPhone control from scratch, or using an existing library or framework.

我已经看过three20库,以及tapku和触摸习俗,这对于专业的iOS控件很好,比如桌面视图等,但我说的是完全定制,互动控制在这里。

I have seen the three20 library, aswell as tapku and touch customs which are nice for specialised iOS controls such as table view etc but I'm talking about making totally custom, interactive controls here.

假设我想制作一个与此应用程序类似的拨号控件: http://store.apple.com/us/product/H2654LL/A

Lets say I wanted to make a dial control similar to the one from this app: http://store.apple.com/us/product/H2654LL/A.

哪里会我开始?


  • 我会将UIView子类化并自定义吗?

  • 我会使用石英2d吗?

  • 我是否会使用OpenGL ES在屏幕上绘制这样的内容?

  • 我是否仍然可以使用IB来设计/布局我的自定义视图?

  • Would I subclass UIView and customize it?
  • Would I use quartz 2d?
  • Would I use OpenGL ES to draw something like this to the screen?
  • Can I still use IB to design/layout my custom view?

我只是有点困惑要走到哪里。

I'm just a bit confused which way to go here.

是的 - 此问题之前已经被问过并回答了几次,但我还没有找到一个满意的答案来解决上述问题。

Yes - this question has been asked and answered a few times before, but I am yet to find a satisfactory answer which addresses the above points.

推荐答案

什么是子类
而不是 UIView 你可能想要继承 UIControl 。此类具有目标/操作模式构建的功能,您可以使用它来响应自定义控件生成的操作。 UIKit上的大多数元素,如按钮和滑块,都是从 UIControl 继承的。

What to subclass Instead of UIView you would probably want to subclass UIControl. This class has functionality for the Target/Action pattern build in which you can use to respond to actions generated by your custom control. Most elements on UIKit like buttons and sliders inherit from UIControl for this specific reason.

可视化您的子类
绘图实际上取决于您想要实现的目标以及您想要制作动画的部分。您可以使用图像,使用石英或OpenGL绘制,具体取决于您需要什么或您喜欢什么。只需使用该技术以最简单的方式实现所需的效果。多个图像可用于处理不同的状态(按下等)或用于精灵动画。 CALayers很容易旋转或移动。

Visualizing your subclass Drawing really depends on what you want to achieve and what parts you want to animate. You can use images, draw using quartz or OpenGL depending on what you need or what you prefer. Just use the technique to achieve the desired effect in the most simplistic way. Multiple images can be used to handle different states (pressed, etc) or be used for a sprite animation. CALayers are nice to easily rotate or move.

无论您使用何种技术,您都可以使用传入的触摸事件来控制动画。在拨号控制的情况下,您可以根据y坐标移动控制旋转量。例如。

No matter what technology you use, you would probably use incoming touch events to control the animation. In case of a dial control you would control the amount of rotation based on y coordinate movement for example.

举例说明:例如,如果我的控件只需要我使用了图像按下时更改例如:只需交换图像。我也喜欢使用 CALayer ,它为您提供了生成边框,蒙版,渐变和角半径的简单方法,所有这些都可以轻松制作动画。

To illustrate: I for example have used images if my control only needed to change when pressed for example: just swap the images. I also like to use CALayer a lot which gives you easy ways to generate borders, masks, gradients and a corner radius, all easily animated too.

在Interface Builder中使用
通过桌面上的Cocoa,可以为自定义控件构建自定义IB调色板。 iOS从来没有这个功能,我不认为IB插件可用于Xcode 4.

Using in Interface Builder With Cocoa on the desktop it was possible to build custom IB palettes for custom controls. iOS never had this functionality and I don't thing the IB plugins are available for Xcode 4.

所以目前处理自定义子类的唯一方法是使用<$ IB中的c $ c> UIView 并将 Identity Inspector 中的自定义类字段设置为自定义类的名称。这样您就可以获得布局和大小的视图。在Interface Builder中,它只是一个矩形,在运行您的应用程序时,XIB实际上会将该视图反序列化为您的自定义类。

So the only way to handle custom subclasses currently is by using a UIView in IB and setting the 'Custom class' field in the Identity Inspector to the name of your custom class. This way you have a view you can layout and size. In Interface Builder it's just a rectangle, when running your app the XIB will actually deserialize that view to your custom class.

使用 UIControl时/ code>你可以免费获得目标/行动机制。因此,您可以将触摸事件连接到IB中的任何对象,就像使用任何其他标准UIKit控件一样。

When using a UIControl you get the target/action mechanisms for free. So you can wire up your touch events to any object in IB just like with any other standard UIKit control.

有一点需要注意:如果您有自定义 - initWith ....:选择器,不会被调用。您的类是从XIB反序列化的,因此您应该使用 - initWithCoder:(NSCoder *)aDecoder; 作为初始化。

One thing to note: if you have custom - initWith....: selectors, those will not be called. Your class is deserialized from the XIB so you should use - initWithCoder:(NSCoder *)aDecoder; as initialization.

这篇关于iPhone / iOS自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆