0

I am working with core-graphics and in my app I am drawing different type of shapes and manipulating them. So I have around 150 UIView objects(shapes) which can be chosen by user.

I have a NSObject class, which has sliders to change the size of shapes. So I am passing the view instance from every UIView to this NSObject class to modify there size.

In NSObject class I have a function like below:

-(id) initWithView:(id)shapeView
{
   ...
}

So at run time when user choose a shape and move the slider that shape's instance is passed to this function in NSObject class. Now I can manipulate the size of the particular "shapeView".

But the problem is I can't access functions of UIView class to which it belongs.

I can't use something like below because type of shapeView is unknown at compile time and it throws error:

[shapeView someFunction:];

For example user chooses a Rectangle in app. Then the Class type of shapeView is Rectangle. I can find out this using [view class]. So I can modify the size of rectangle but can't access its functions. To access function I have to declare a instance of type Rectangle in NSObject class. So my question is how to extract the Class name from a instance (shapeView in this case) and declare a instance of that class type.

I know one way is to check type of every type of UIView class and then make instance accordingly. But as I said there are 150 UIView classes and in future I will add more to it. So is there any other way around to solve this problem?

blancos
  • 1,576
  • 2
  • 16
  • 38
  • You are receiving the view in argument, why do you need to create the instance? – Apurv Jun 16 '14 at 07:16
  • I am fairly certain that making these shapes a subclass of `UIView` is the wrong approach. Better is to create a class called `Shape` (a subclass of `NSObject`) with properties like `position` (`CGPoint`) and a method called `draw` (which is called when drawing the parent `UIView`) and then deriving shape primitives from this `Shape` base class. – trojanfoe Jun 16 '14 at 07:17
  • You can have overlook for the Runtime.h file. It is having all the runtime functions set. – Apurv Jun 16 '14 at 07:20
  • @trojanfoe : I thought about that but the thing is, this is just the part of app. I have many touch gestures associated depending on type of shape like rotation, panning and change of orientation with pinch etc etc. Are these events possible without a UIView? – blancos Jun 16 '14 at 07:46
  • @blancos OK good point. However a layer would perhaps be slight less expensive, but I am unsure of how much less expensive. – trojanfoe Jun 16 '14 at 07:48
  • @trojanfoe : I have considered using layers but CALayers has no built-in support for user interaction. Also they don't handle hit-testing on touches. – blancos Jun 16 '14 at 07:56

1 Answers1

0

I don't think it was a good idea to have a subclass of each individual shape. It would have been better to declare an class like Polygon then declare properties such as numberOfSides and area even NSArray* edges or NSArray* angle if you have an irregularly shaped polygon. Then you'd have a method drawRectangle which would take that data and render it on screen based on the frame it has.

Circular and Composite could have been the other classes with composite being any odd shapes that are made up of 2 more separately defined shapes.

I would recommend you rethinking your design to something more manageable. You should never have to maintain that many classes, it's why we use Polymorphism in the first place.

Community
  • 1
  • 1
Literphor
  • 498
  • 4
  • 16
  • Agreed. But as I mentioned in comments I have many touch gestures to be handled, based on the shape. – blancos Jun 16 '14 at 07:50
  • These touchgestures can also be implemented in the class and the behavior based on number of sides and angles. What problem do you have that you feel you need to have that many subclasses? – Literphor Jun 16 '14 at 08:12
  • @blancos When rotating, I would just rotate using the superclass (UIView) implementation. That will rotate the entire view, which is better than rotating the content of the view (which I'm asssuming is what you're doing). Panning and orientation can be done the same way where you're panning the view itself. – Literphor Jun 16 '14 at 08:14
  • Touch gestures only work on UIView. And your approach is not considering multiple shapes at a time. Rotating superclass UIView will rotate the all the shapes present at that point. And user cannot work on multiple shapes shapes simultaneously as all shapes are in one view. – blancos Jun 16 '14 at 08:36
  • @blancos Your shapes **are** UIViews aren't they? That means they are themselves a view, and as a view they support things like rotation and touch gestures. Don't confuse `super**class**` with `super**view**`. If you rotate the `super**view**` then yes it would also rotate all of its sibling views. But if you rotate the shape (the view) using it's `super**class**` implementation, then it will only rotate that shape (the view). – Literphor Jun 16 '14 at 08:49
  • I am clear with that concept. You're not considering every thing I mentioned in question's comments. I have touch gestures and some specific functions related to orientation(different types of manipulations) etc which are unique to every shape. To implement those I have to subclass UIView and write functions for it. So subclassing is a necessity for me. I have consulted many times about my design. With all these requirements, its difficult without subclassing UIView. – blancos Jun 16 '14 at 08:53
  • @blancos I just fail to see anything so far that can only be solved by multiple subclasses. Lets say you have a `UITapGestureRecognizer` and want it to either pan, rotate, translate, whatever based on which shape it is of 150+ shapes. Just off the top of my head I'd have an `NSArray` of 150 slots each with a block containing the logic for whatever shape it belongs to. When the `handleTap` selector is called, I'll animate the view by getting the `numberOfSides` it has and accessing the behavior from the `NSArray`. – Literphor Jun 16 '14 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55687/discussion-between-blancos-and-literphor). – blancos Jun 16 '14 at 09:06