swift - making an image the full size of a scrollview -
i have set image background in scrollview can zoom in pan around it, however, when image zoomed out creates white borders left , right of image can assume scrollview margins.
any way rid of these image fills screen?
edit: here picture show mean, white bars along sides of grey background edges of scrollview, want grey background fill screen apart iad bar. http://i.stack.imgur.com/qx0kk.png
many thanks!
// 1 let image = uiimage(named: "photo1")! imageview = uiimageview(image: image) imageview.frame = cgrect(origin: cgpointmake(0, 0), size:image.size) // 2 scrollview.addsubview(imageview) scrollview.contentsize = image.size // 3 var doubletaprecognizer = uitapgesturerecognizer(target: self, action: "scrollviewdoubletapped:") doubletaprecognizer.numberoftapsrequired = 2 doubletaprecognizer.numberoftouchesrequired = 1 scrollview.addgesturerecognizer(doubletaprecognizer) // 4 let scrollviewframe = scrollview.frame let scalewidth = scrollviewframe.size.width / scrollview.contentsize.width let scaleheight = scrollviewframe.size.height / scrollview.contentsize.height let minscale = min(scalewidth, scaleheight); scrollview.minimumzoomscale = minscale; // 5 scrollview.maximumzoomscale = 1.0 scrollview.zoomscale = minscale; // 6 centerscrollviewcontents() } func centerscrollviewcontents() { let boundssize = scrollview.bounds.size var contentsframe = imageview.frame if contentsframe.size.width < boundssize.width { contentsframe.origin.x = (boundssize.width - contentsframe.size.width) / 2.0 } else { contentsframe.origin.x = 0.0 } if contentsframe.size.height < boundssize.height { contentsframe.origin.y = (boundssize.height - contentsframe.size.height) / 2.0 } else { contentsframe.origin.y = 0.0 } imageview.frame = contentsframe } func scrollviewdoubletapped(recognizer: uitapgesturerecognizer) { // 1 let pointinview = recognizer.locationinview(imageview) // 2 var newzoomscale = scrollview.zoomscale * 1.5 newzoomscale = min(newzoomscale, scrollview.maximumzoomscale) // 3 let scrollviewsize = scrollview.bounds.size let w = scrollviewsize.width / newzoomscale let h = scrollviewsize.height / newzoomscale let x = pointinview.x - (w / 2.0) let y = pointinview.y - (h / 2.0) let recttozoomto = cgrectmake(x, y, w, h); // 4 scrollview.zoomtorect(recttozoomto, animated: true) } func viewforzoominginscrollview(scrollview: uiscrollview) -> uiview? { return imageview } func scrollviewdidzoom(scrollview: uiscrollview) { centerscrollviewcontents() }
you setting min zoomscale min of width , height scales. desired, allows see entire image (aspectfit). sounds want aspectfill though, that, need use max of width , height scales.
let minscale = max(scalewidth, scaleheight);
Comments
Post a Comment