Reposition UIButtons after zoom - iphone

I have a scrollview with an imageview on it. I am able to zoom the image view placed inside the scrollview. On top of it I have some uibuttons, which do not reposition after zoom.
How do I reposition UIButtons after zoom?
Please help
Thanks

One way would be to implement the scrollViewDidZoom: delegate method of your scroll view delegate, determine the currently visible rectangle using contentOffset and contentSize properties of your UIScrollView, compute the new location of your buttons relative to the visible rectangle of the scroll view, and reposition your buttons into that rectangle by setting their frame property as desired.

As Fisk mentioned, the UIScrollViewDelegate should help. One of the delegate methods is:
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
If you implement the UIScrollview delegate this method should get called when your scrollview is done zooming. So in this method you can reposition your button after the zoom:
[yourButton1 setCenter:CGPointMake(newX,newY)];
Good luck.

Your question is a bit vague. However, whatever it is that you are doing, I believe UIScrollViewDelegate can help you.
This is a delegate for the scroll view that lets you know when it is scrolling. I never got where the buttons are supposed to be, but with this delegate I guess you can simply place them wherever they need to be.

Related

On zoom, UIScrollview subviews not recognizing gesture

I have a scrollview to which I have added an image view as a subview. The contentsize of the scrollview is same as the size of the imageview (width and height). There are two buttons outside the scrollview. When I click on either on them, a small rectangle view is added as a subview to the imageview. This rectangle view can be dragged anywhere on the imageview by the user after it has been added.
This works fine until I pinch to zoom the scrollview. In the viewForZoomingInScrollView: method, I return the imageview and the zooming works fine. But in the zoomed view when I try to drag around the rectangle view, it does not move. It does not recognize the pan gestures any more. Any idea why this is happening?
Thanks
Hetal
Following up to your last comment, I'd suggest to try the following:
In your viewDidLoad method add this:
for (UIGestureRecognizer *gr in self.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPanGestureRecognizer class]])
{
[gr requireGestureRecognizerToFail:self.dragGR];
}
}
where dragView is your rectangleView.
The following is probably not necessary for the above to work, but it might be good practice to clean up the view hierarchy anyway:
Add a plain UIView as superview of your image view with the same frame and add the rectangle view as subview of that new view. In your viewForZoomingInScrollView: return that new view. As I said in the comments I don't think adding subviews to UIImageViews is considered good practice. But maybe that's just my opinion :)
This should work as well and is slightly more concise:
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.myPanRecognizer];

Can't override "hitTest" method for UIScrollView

I've created a scroll view, width less than the screen width and set clipsToBounds=YES. As the UIScrollView doesn't scroll from anywhere outside the range of its frame, I put the UIScrollview inside a UIView and tried to override the "(UIView *)hitTest:WithEvent:" method. But it always shows warning pointInside method not found (or something like that) in the if ([self pointInside:point withEvent:event]) line and it doesn't work. What did I do wrong? Thanks in advance...
You have to change the ScrollView's contentSize in order to allow the ScrollView to scroll. It will only scroll it the contentSize.x is bigger than scrollview.size.x and/or contentSize.y bigger than scrollview.size.y.
Answering my own question, the problem is I didn't create the UIVIew subclass to override the (UIView *)hitTest:WithEvent: while the UIScrollView was in a subview of self.view. So, it is important to check which view inherits the scroll view and write overridden method in the parent view class.

Overlaying a UIScrollview without cutting off touch events to the scrollview

I have a transparent overlay that I'd like to put over a UIScrollview. I'm adding it as an Imageview sibling view to the scrollview so that it remains stationary while the scrollview subviews move freely underneath. The problem is that views pass their events to the superview, not the siblings. IS there a way to pass events from this overlay to the scrollview? Or can anyone think of a better way to achieve the same effect? Thanks!
This should Just Work, as long as the UIImageView has its userInteractionEnabled property set to NO: the superview sends -hitTests:withEvent: to its subviews in order, and the UIImageView should return nil, whereas the UIScrollView should return itself (because it has gesture recognizers).
If it's not working for you, the chances are that your view layout is not what you think it is. UIView has a useful method called -recursiveDescription which you should call on the superview and NSLog the result.

Can a UIScrollView be adapted to zoom horizontally but not vertically? (how)

I'm trying to make a timeline view that shows events as UIButtons that can be pressed for more info.
Ideally I want to lay out the buttons on a UIScrollView, and allow them to stretch/shrink horizontally, but not vertically.
What I'm trying to accomplish would basically be equivalent to using pinch gestures to resize the content view, and using a scroll view to move side to side - could you point me in the right direction, or suggest how you might accomplish something like this?
My first thought is a UIScrollView to scale a UIView subview, and have the UIView subview contain another subview that does not resize vertically, but only does horizontally when bounds change, and disable vertical scrolling. Maybe I could skip one of these UIView subviews and have only one do everything. It just feels like I'm trying to hack this together like an HTML page or something with all these containers.
Not sure if I've explained any of this well enough to hope for an answer, any help is appreciated though.
I've implemented horizontal-only scaling by creating a subclass of UIView that simply overrides setTransform and sets the Y scale to 1 whenever the UIScrollView changes the scale:
#interface DiagramStripView : UIView { }
#end
#implementation DiagramStripView
- (void)setTransform:(CGAffineTransform)newValue;
{
newValue.d = 1.0;
[super setTransform:newValue];
}
#end
As the class name suggests, my view holds a series of diagrams that are each one screen wide. When the user lets go, the view controller resets the view's scale to 1 and redraws everything to the new scale:
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view atScale:(float)scale
{
diagramStripView.transform = CGAffineTransformIdentity;
[self redrawDiagrams:scale];
}
Haven't tried this, but you could try placing everything in a UIScrollView, and whenever you detect a zoom level change, adjust all of the child view transforms to CGAffineTransformMakeScale(1.0, 1.0/zoomLevel). This way, the scrollview is zooming everything up, but each subview is scaling themselves vertically downward, canceling out the zoom.
Assuming you don't actually want to stretch/shrink the button text, the easiest way is to resize all the buttons. I know, it's a pain.

how to pinch image in iphone

Hi I am new i want to develop a application that we zoom in and zoom out image but i am not understand how to do this. any one help me with code and other
For iPhone OS 3.2 or higher, use the bundled gesture recogniser UIPinchGestureRecognizer.
In my opinion the easiest way would be to add an UIScrollView to your viewcontroller which will be the scrollviews delegate.
Then you'll implement the viewForZoomingInScrollView UIScrollViewDelegate method where you'll return the UIImageview that has to be zoomed/pinched.
You would also need to set the maximumZoomScale and minimumZoomScale properties of the UIScrollView.
UIScrollView documentation
UIScrollViewDelegate documentation
A scroll view also handles zooming and panning of content. As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should should update subviews of the content as necessary. (Note that the gesture can end and a finger could still be down.) While the gesture is in progress, the scroll view does not send any tracking calls to the subview.
The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.