简体   繁体   中英

Block size image when I enlarge the view

I have a view and within an image that works as a button. I would like to know if there is a way to lock the size of the button so that when I zoom in, the view remains small and does not enlarge with the view..

一

二

I thought it was hard to manager in the beginning. But finally, if you put the imageView in a UIScrollView, It's not hard to achieve.

The idea is move the buttonView outside of imageView during zooming and when zoom is over, put it back to imageView to pretend nothing happened. I know it's too verbose in programming but actually it works perfectly for your case.

    var  originalCenter : CGPoint! // The center of ButtonView in imageView.


   //All functions are from the  UIScrollViewDelegate.

            func viewForZooming(in scrollView: UIScrollView) -> UIView?{
             originalCenter  =  buttonView.center // remember the original position.
            return imageView
        }

            func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
                buttonView.frame =  imageView.convert(buttonView.frame, to: scrollView)
                scrollView.addSubview(buttonView)//add to superView of imageImage.
            }

            func scrollViewDidZoom(_ scrollView: UIScrollView){
                buttonView.center = imageView.convert(originalCenter, to: scrollView)  //During Zooming, update the buttonView in ScrollView.
            }

            func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat){
                buttonView.frame = imageView.convert(buttonView.frame, from: scrollView)
                imageView.addSubview(buttonView) //put it back.
            }

I know it's better to use a parameter to control such operation. But I have-not found one according to public APIs. Maybe there is a better way, hope this one is your answer too.

Because you called transform for superView. It will make all subViews inside transform together.

You need to remake you views:

SuperView:
- Content view (the image view)
- Border view
- Button close

When you want to zoom the image, you only need to reset the superview frame.

You found a git SPUserResizableView .

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