[IOS development-54] Case Study: using the UIScrollView's Zoom image function to practice the specific implementation of the proxy mode,-54 uiscrollview
Case: (in the simulator, press the option key and click the mouse to see the scaling gesture)
(1) In ViewController. m:
-- Zooming is another function of UIScrollView in addition to scrolling. Therefore, you should first put the zooming in UIScrollView, for example, the imageView here;
-- When scaling, scrollView itself does not know which control we want to scale. Therefore, scrollView requires a proxy to tell it that this proxy is generally our controller;
-- The controller must abide by its Protocol to become its proxy;
-- After the Controller accepts the protocol, it can call this method to tell scrollView which control needs to be scaled;
-- Scaling is not allowed at this time, because the maximum and minimum scaling ratio of the scrollView is not set, and there is no boundary for scaling.
# Import "ViewController. h "@ interface ViewController () <UIScrollViewDelegate> // The first step is to comply with the Protocol @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (weak, nonatomic) IBOutlet UIImageView * imageView; @ end @ implementation ViewController-(void) viewDidLoad {// self. scrollView. contentSize = CGSizeMake (892,632); self. scrollView. contentSize = self. imageView. frame. size; // sets the maximum and minimum scale of zooming. scrollView. maximumZoomScale = 2.0; self. scrollView. minimumZoomScale = 0.2; // sets the UIScrollView proxy self. scrollView. delegate = self; // step 2, set yourself as the proxy [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // to implement this method, the proxy actually needs to return the control to be scaled to the scrollView itself-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {return self. imageView;} @ end
(2) How do I imitate the scaling gesture in the simulator?
Press and hold the option key, and click the mouse to show a scaling gesture. Dragging is equivalent to scaling.
(3) Description
Proxy, the most important role is listening. That is, the proxy can be directly notified of any changes to this control. The proxy can immediately know and respond accordingly.
Of course, some methods in this protocol must be implemented, that is, selective implementation.
Why can't I scale images in IOS? UIScrollView points
To set proxy-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView;
You can scale the view to be scaled as the return value. I just started to learn this. I just checked it online for you. This should be just a proxy method. Others can check UIScrollViewDelegate by themselves.