How to code zooming and panning for a UIImageView? - iphone

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/

Related

How to make certain part of image clickable in ios?

In one of my app i am using image for the whole screen. Which can be zoomed to some extent. That image has eight different shapes(includes person,shapes,etc).What i am trying to do is i need to make certain each shape of the image is clickable. Touching each part takes to different screens.I didn't have any idea about how to achieve this. I googled it but no solution.
1.) Is this possible by using co-ordinates(will normal image and zoomed image differ in co-ordinates? How to achieve this by using co-ordinates?
2.) If not what will be the best approach to achieve my goal?
Any ideas/samples is much appreciated.
I would add a UITapGestureRecognizer to the imageView holding your image. And the locationOfTouch:inView: method to determine the coordinates of your touch.
Correct me if i don't understand your question. For me, that should be very simple? Just have couple of buttons which background is clear? And They are all on top of the image.
Check UIResponder and the touches methods in there. You'll probably want to hook in to something like -touchesEnded:withEvent: for detecting when a finger lifts off the screen.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGRect touchLocationInView = [touch locationInView:imageView];
// Do something to check that the rect is valid
// If valid, react to it
}
}
Also, a link to UITouch.

how to get touch coordinates in an image then draw a markup icon and its content in a popover view

I would like to built an iPhone/iPad application to show large images (in a scrollView or something else which support dragging and zooming) that allow user to:
Touch some where in the image to markup and leave comment
User can tap on that markup icon/button to view comment in a popOverView
Edit comment or remove that markup
So I want to ask that:
How can I get the touch coordinates in image (not screen)?
How can I draw a markup icon/button at touch point in the image and it would follow image even when dragging, zooming since the image is really large, maybe up to 8000x6000 pixels?
How can I display comment/note when user touch on markup icon/button in a view like popOverview in iPad?
Save and load these information.
It is nearly similar to tagging functionality of Facebook App in iPhone.
Any help is appreciated, thank in advance!
1 . You subclass the UIImageView and override the touch methods:
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
2 . You add and UIButton to the UIImageView [yourImageView addSubview:yourButton]; then set the center of your button to touch coordinates.
3 . Just present an popover when user taps on an button. (You can set tag property for buttons to know which button is tapped)
4 . Save data to an plist to documents directory if is not to complex ore use core data.
Good Luck. Just post comments if you need more help.
Edit:
you need to set the user userInteractionEnabled to YES for the UIImageView.
userInteractionEnabled A Boolean value that determines whether user
events are ignored and removed from
the event queue.
#property(nonatomic,
getter=isUserInteractionEnabled) BOOL
userInteractionEnabled Discussion This
property is inherited from the UIView
parent class. This class changes the
default value of this property to NO.
Availability Available in iOS 2.0 and
later. Declared In UIImageView.h
From UIImageView Class Reference

How to detect touch on UIImageView inside UIScrollview?

I have a UIScrollview in my app and I populate it with LOTS of UIImageViews approx 900. They are all very small and consist of only two different images over and over again.
Now I am having a lot of trouble detecting a touch on one of these UIImageViews.
I have assigned them all a unique TAG so as to be able to distinguish between them but I am really struggling to detect the touch.
The goal is just to be able to change the image of the touched UIImageView.
Due to the large amount of views involved a simple loop checking touch coordinates against each UIImageViews frame is just hanging my app.
Does anyone have any suggestions?
Thanks in Advance.
Ben
In my case the easiest way to do it was adding a gesture recognizer:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
//Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
singleTap.cancelsTouchesInView = NO;
[myScrollView addGestureRecognizer:singleTap];
Then use this method to capture the touch:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:myScrollView];
}
UIImageView has touch processing turned off by default. To change that set userInteractionEnabled to YES (see the docs here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)
Also if you want to know which view was hit in a view hierarchy you can use hitTest:withEvent: (see docs in UIView) which will be much faster than you looping through the hierarchy.
On a wider note I don't think having 900 UIImageView's on the screen all at once is going to be a good long term strategy. Have you investigated drawing the screen's content via CoreGraphics?
The best way to solve this issue is to subclass UIScrollView, add touchesBegan etc methods to it.
#interface MyScroll : UIScrollView
{
}
You could easily implement this method on the UIImageView object:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
//You clicked inside the object
}
return [super hitTest:point withEvent:event]
}
Use the return to tell the application the next touch responder (such as the uiimageview or the scrollview).

Zoom multiples UIImageViews inside an UIScrollView

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.)

Two related questions about iPhone autorotation

1) Is it possible to change the speed of autorotation on the iPhone?
2) Is it possible to time an animation to take place during the autorotation animation? I would like to resize a photo while rotation is occuring, rather than when it's done.
1) not that I know of
2) Yes, see this method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW53
As for the first question, I haven't used this but check it out:
[UIApplication sharedApplication].statusBarOrientationAnimationDuration = [NSTimeInterval ...];
See docs for UIApplication. The problem is that I don't know that this actually rotates the rest of your views. You may, however, be able to rotate the window's coordinate system around "manually" using UIWindow and [UIView beginAnimations ...] in order to recreate the whole effect.
Please post sample code somewhere if you get it working!