简体   繁体   English

UIView或UIViewController子类?

[英]UIView or UIViewController subclass?

I'm trying to refactor my code in the best possible way and I'm wondering what the proper architecture for the given situation is. 我试图以最好的方式重构我的代码,我想知道给定情况的适当架构是什么。

What I'm Trying To Do 我正在尝试做什么

What I'm doing is pretty simple: I have some custom CALayer subclasses that represent an interactive UI element. 我正在做的很简单:我有一些代表交互式UI元素的自定义CALayer子类。 They are broken up into multiple layers since some of the parts of the UI are static, so I didn't want to redraw those static elements needlessly. 它们分为多个层,因为UI的某些部分是静态的,所以我不想不必要地重绘这些静态元素。 Right now, the layers are added as sublayers in the initialization part of a CustomView class that is a subclass of UIView . 现在,这些图层作为子图层添加到作为UIView的子类的CustomView类的初始化部分中。

There is currently no corresponding CustomViewController class that is a subclass of UIViewController because when I'm using the CustomView , it's contained within a UITableViewCell or a part of a generic UIViewController with other views in it, so I felt another UIViewController for each CustomView instance would be redundant. 目前没有相应的CustomViewController类是UIViewController的子类,因为当我使用CustomView ,它包含在UITableViewCell或其中包含其他视图的泛型UIViewController的一部分中,所以我觉得每个CustomView实例的另一个UIViewController会多余的。

Also of importance is the that the only operation that I'm doing inside of the UIView class is I'm responding to touch events and sending the touch information to the sublayers so that the UI can update its appearance accordingly. 同样重要的是,我在UIView类中进行的唯一操作是响应触摸事件并将触摸信息发送到子层,以便UI可以相应地更新其外观。 I'm not overriding the drawRect method or anything like that. 我没有覆盖drawRect方法或类似的东西。

The Question 问题

Basically, I'm trying to figure out whether I should either: 基本上,我想弄清楚我是否应该:

Option 1: 选项1:

Get rid of the CustomView class, create a CustomViewController class that is a subclass of UIViewController , and simply add the CALayer objects as sublayers of the CustomViewController 's built-in view property. 摆脱CustomView类,创建一个作为UIViewController的子类的CustomViewController类,并简单地将CALayer对象添加为CustomViewController的内置view属性的子层。

or 要么

Option 2: 选项2:

My thinking about the UIViewController subclass being redundant is correct, so I should leave it the way I have it and have a CustomView class with the CALayer objects inside of it. 我认为UIViewController子类是多余的是正确的,所以我应该按照它的方式保留它并且有一个带有CALayer对象的CustomView类。

I would highly appreciate any advice on this. 我非常感谢有关这方面的任何建议。

I think that in terms of MVC, the code you're describing (option #2) is well written and maintains a very clear boundary of responsibility. 我认为就MVC而言,您所描述的代码(选项#2)编写得很好并且保持了非常明确的责任范围。 You're not writing any code that has nothing to do with the view layer itself in this class which is great. 你没有在这个类中编写任何与视图层本身无关的代码。 I think that in this case there's no need for a separate UIViewController subclass to manage these instances because as you said - they are handling their own touch events and visible layers (exactly their responsibility). 我认为在这种情况下,不需要单独的UIViewController子类来管理这些实例,因为正如你所说 - 他们正在处理他们自己的触摸事件和可见层(确切地说是他们的责任)。

If for any reason there is a need for something more complex that requires data related logic or other such computation, I would definitely consider subclassing a UIViewController or maybe looking at the problem in an entirely different way. 如果由于任何原因需要更复杂的东西需要数据相关的逻辑或其他这样的计算,我肯定会考虑子类化UIViewController或者以完全不同的方式查看问题。

Given the situation you've presented, I think that maintaining the CALayer instance within this UIView subclass ('CustomView') is the right way to go. 鉴于你提出的情况,我认为在这个UIView子类('CustomView')中维护CALayer实例是正确的方法。

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

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