Zoom multiples UIImageViews inside an UIScrollView - iphone

I am trying to create a small game for the iPhone with images and I want to them to zoom in when the player is pinching on the screen. I have seen the tutorials with one UIImageView. However, now that I am using multiple UIImageViews, things do not work OK.
I put them in an UIView and I am trying to zoom the UIView. However things are not scaling inside the screen when I run the simulator.
I use the following code
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return containerView; //containerView is the UIView containing all the UIIMageviews
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
[containerView setTransform:CGAffineTransformIdentity];
containerView.contentScaleFactor = scale;
}
Any suggestions?
Thanx!

Have a look at the ScrollViewSuite sample on the apple developer site, that has a working example of what I think you're trying to do.
(I think I got the name right, the developer site is down for updates just now so I can't give you a link.)

Related

how to add pinch zoom effect to the images parsed from web services and added on scrollview?

I have a scrollview and i added the images from webservice. Now i want to add pinch zoom effect to the images. But when i am going to zoom in image becomes full in size and exception is throwing NSRangeException.So guys please help me.Thanks in advance.
Use following method in your code to allow zoom -
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [self.scrollView.subviews objectAtIndex:0];
}

Add Zooming to UIScrollView

I've been doing the tutorials in the Big Nerd Ranch Iphone development book and ran into an issue I can't solve or find an answer for.
The App (Hypnosister) basically just draws a bunch of concentric circles and places some text on the screen using the DrawRect method. I got that to work and I was able to add scrolling ot the view but can't add the ability zoom. I've checked my code 100 times and can't figure it out. I think they used an earlier version of the OS and I am using 4.2. Did something change? Can you find the issue?
Here is the relevant code:
#interface HypnosisterAppDelegate : NSObject <UIApplicationDelegate,
UIScrollViewDelegate>
{
UIWindow *window;
HypnosisView *view;
}
and this goes in the application didFinishLaunchingWithOptions part:
[[UIApplication sharedApplication]
setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
and method gets called after that method closes:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view;
}
HypnosisView is a UIView class that does the drawing. Thanks for the help.
Just downloaded the examples for the book from the Big Nerd Ranch website and it works fine on the simulator under 1OS 4.3. Did you add these lines of code to didFinishLaunchingWithOptions:
// Enable zooming
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:5];
[scrollView setDelegate:self];
This is only possible problem I can think of based on the code you've shown.

Vector like drawing for zoomable UIScrollView

I have a zoomable UIScrollView with some CATextLayers and simple CALayers in it.
They get rendered fine but the problem is when they are zoomed they become blurry. (Even if I redraw them)
What would be my options to get my text not blurry when the view is zoomed?
Should I use another thing? enable something in CATextLayer/CALayer? Any idea is welcomed ;)
I am using CALayers because, as suggested in documentation, CALayers are lighter than UIViews and I have hundreds of them. Currently it works smoothly. I have tried with UIWebView and my CALayer version is faster ;)
Thanks in advance.
iOS rasterizes the text before the scale-up occurs, that's why it's so blurry. You only need to fix one property of your CATextLayer, contentsScale, to get a higher quality render after zooming. Implement this UIScrollViewDelegate method:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view
atScale:(float)scale
{
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithBool:YES]
forKey:kCATransactionDisableActions];
uglyBlurryTextLayer.contentsScale = scale;
[CATransaction commit];
}
This tells the layer to use more pixels to render the text, and it disables Core Animation when making that particular change.

Lazy load pages in UIScrollView

I have a UIScrollView that contains large images and am using paging to scroll between images. In order to save memory, I am loading only one image before and after the currently visible one and loading/releasing new images after a scroll has completed.
The problem occurs when one scrolls quickly and scrollViewDidEndDecelerating is not called.
How can I detect continuous scrolling?
I could check item location on every scrollViewDidScroll but this seems a bit heavy...
Perhaps a table view with custom cell content would work better for you, since it has a lot of logic built in for only loading cells that are visible as opposed to everything at once. There are lots of tutorials for how to manage table views in various ways.
My temporary solution is to cancel continuous scrolling by disabling scroll from when the user lifts the finger until scroll has completed.
-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
[scrollView setScrollEnabled:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[scrollView setScrollEnabled:YES];
}
if you are still looking for a solution... this what I do, it works really good having a good performance, too.
- (void)scrollViewDidEndDragging:(UIScrollView *)sv willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
// load images and unload the ones that are not needed anymore
[self someMethod]
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv
{
// load images and unload the ones that are not needed anymore
[self someMethod]
}
You have to check both scrollViewDidEndDecelerating and scrollViewDidScroll.
When to user swipes and releases it letting to have the "momentum" scroll the first will be called at the and. If the user decides to stop the scroll with his finger by tapping to the tableview (or scroll comes to the most bottom, but I am not sure about that) the second event is fired.
You can do something like this
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 1.5) / pageWidth) + 1;
}
this method is called anytime the contentoffset changed whether programmatically or not.
From this method you can check to see if 'page' is the same as your current page, if not, you know you need to load something. The trick here is making images load without holding up the scrolling of the scrollview. That is where I am stuck.
the page calculation will change to next/previous when you are half way to the next page
As per my experience, #jd291 is correct; I am using following callbacks successfully for most places
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
but, the only case when a callback is not called is that you may not had set up the delegate properly.
The WWDC 2010 has talked about this. They do a reuse of the sub scrollviews, alloc the missing image view in scrollViewDidScroll:. The video may help:
Session 104 - Designing Apps with Scroll Views

How to code zooming and panning for a UIImageView?

I have a UIImageView object, attached to a controller. It displays fine. What is a easy way to get zooming and panning with the least amount of code? Perhaps some library out there that does this? Hard to believe the SDK does not provide anything.
Add your UIImageView as a subview of a UIScrollView and make sure you change the minimumZoomScale or maximumZoomScale.
Also take a look at the documentation for UIScrollView there might be other settings you want to tweak.
UIScrollView is the SDK class you're looking for
The Three20 project has a photo viewer you may want to look into, that I believe supports zooming and panning in a larger image.
try using
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
Here is some code
http://www.redcodelabs.com/2009/09/objective-c-zoom-image/