简体   繁体   中英

objective c change size of subview

I'm pretty new to this and I don't completely understand all of the inner workings of Objective-C, so I was hoping someone could help me out.

Essentially I have a Person class, where each person is represented by a group of objects (a button, label, and image view) that appear and disappear at certain times throughout the implementation. Occasionally, I want the person to move, and the best way I have figured out so far is to treat all three objects separately and animate them at the same time.

I figured there must be a better way to do this, and I am wondering if it is possible to create something like a "subview" (I don't know if this is the right word for it) and insert the button/label/imageView onto that and then animate the subview. I would imagine it would be something like:

Person *person = [[Person alloc] init];
person.view.frame = CGRectMake(x, y, width, height);
[self.view addSubview:person.view];

But this doesn't seem to work. Any suggestions?

Thanks in advance!

OK so I'm afraid you need to change your design a bit. Apple work on an MVC architectural design which is really important to adhere to when it comes to making Apps.

What you've got here is a mixing between the model and view which is exactly what isn't supposed to happen with MVC.

Essentially you've got:

1) Model layer which is where you have all your data and objects for encapsulating information, just like your Person class.

2) View layer which is for controlling and defining UI elements of your apps like UIView , UIImageView etc

3) Controller layer which is the interface between Model and View layers. This include classes like UIViewController which manages a UIView .

In your situation you need a UIView into which you can put all the relevant UI (buttons, images) that tell the user about a person. Then you need a Person class. Probably a subclass of NSObject that defines the information for each Person in your app eg maybe a UIImage property that can be displayed in a UIImageView . And then a controller (I would recommend UIViewController ) which would instantiate the relevant Person objects and then update the UI as required.

I'm not entirely sure what you want in terms of animation but you can look at this for information on how to animate views. And check out:

[UIView animateWithDuration:1
                 animations:^{
    // Cool animations here...
}];

I would get to grips with MVC and other key concepts before getting too snazzy though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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