0

In my application I have a UIView. User can rotate, change size, change position of this UIView. I want to capture every state of the UIView and save it in an array.

For example, user moves view, then rotate view, then resize view. I want to save all these 3 views in an array:

[mutableArray addObject:view1]; //when user moves
[mutableArray addObject:view1]; //when user rotate
[mutableArray addObject:view1]; //when user resize

But when I do this I'm getting the same UIView at its current state. How can I capture all 3 states of this UIView?

Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
  • 2
    You should store the properties that you want to save in the array, not the views themselves. EDIT: See http://stackoverflow.com/a/13756101/1305067 for how to copy, if that's what you're after. – paulvs Jul 20 '15 at 14:47
  • @paulvs I understand. But isn't there a way to capture the `UIViews` ? Thanks, i will check out that link – Ty Lertwichaiworawit Jul 20 '15 at 14:51

2 Answers2

0

If you want to capture and save a view on the array, you need to instantiate a new object. What you are doing now is just creating shallow copies, that is saving the pointer to the same object, instead of saving a pointer to a copy of the object.

You should do:

[mutableArray addObject:[view1 copy]];

This will create an exact copy of the object and add it to the array.

This is not the recommended method for what you want to achieve. You should, like comments have stated, save the proprieties of the object and recreate when needed and not save the full view-object in the array.

nunofmendes
  • 3,731
  • 2
  • 32
  • 42
0

Depends what you want to do with the saved views. All that your code is doing is saving references to the view in the array. You may want to copy the object as nunofmendes suggests or copy the properties as paulvs suggests. But if you want to capture what the view looked like at that moment, use [view snapshotViewAfterScreenUpdates].

[mutableArray addObject:[view1 snapshotViewAfterScreenUpdates:YES]]; //when user moves
[mutableArray addObject:[view1 snapshotViewAfterScreenUpdates:YES]]; //when user rotate
[mutableArray addObject:[view1 snapshotViewAfterScreenUpdates:YES]]; //when user resize

You will end up with an array of view objects.

Dan Loughney
  • 4,647
  • 3
  • 25
  • 40