UIScrollview with two images - Keeping 1 image zoomable and 1 image static (fixed size) - iphone

I have a UIView which contains a zoomable UIImageView and also another semitransparent UIView on top of that.
What I am trying to achieve is to be able to zoom the UIImageView while keeping the semitransparent view static and not zoomed.
If I add the semitransparent UIView on top of the UIImageView (which is added to the UIScrollView), everything zooms. However, if I add both as subviews to the base UIView, the touches only get tracked is the semitransparent UIView since its the last one added.
I do need control to reside first at the semitransparent UIView for the touches since I may want to resize the semitransparent view. However, I'd like to pass control of the touches to the UIScrollView if two fingers are used. Is there anyway for me to achieve this? The nextresponder doesn't seem to work. I also tried to use hittest in addition to subclassing UIWindow, but the base UIView needs to push/pop navigation controlling ability so I don't think I can subclass UIWindow to push onto the navigation stack.
Any help would be appreciated.
Thanks,
Winston

Hm.. you can try this hierarchy (possibly subclasses):
UIView (container)
> UIView (semitransparent overlay)
> UIScrollview
- UIView (zoomable content)
Like this, the overlay does not scale.
The tricky thing then is the user interaction on multiple layers. Its easy if there are areas in your overlay that should not detect user touches, for that you just set the UIView property 'userInteractionEnabled' to 'NO' for the view parts where touches should be 'forwarded' to the underlaying layers.
But if I get you right, you need something more complicated. You probably could set up some kind of master-touch-controller in the container UIView, that finds out what is happening and then calls certain methods of its subviews / forwards the events.
I don't know all the exact methods you need to override/implement in the container, but check out the tapZoom demo from the ScrollView Suite sample code. It's a pretty nice example there.
Just out of curiosity, may I ask what this interaction model is used for?

Related

how to drag an uiimage from scrollview to another uiimageview in iphone sdk

In My Application,i am having one scrollVIew containing multiple images.
and out of the scrollview i have one uiimageview.
i want to Drag any image from ScrollView and drop it on uiimageview which is out of the scrollview.
is it possible?
help And suggestions are appreciated
Thanks in advance.
gamozzii's reply is close to what you need to do, but there's one problem. An UIScrollView will eat touches, so tapping on an image will have no effect.
As a result you will have to subclass the UIScrollView
I have written a small functional app to illustrate dragging the image from a scroll view into an image view. You can download the project here.
Yes this should be possible, you will need to implement the touch/drag events directly.
Check out the touchesBegan, touchesMoved etc. delegate methods in UIResponder.
One approach would be to subclass imageview and implement touchesBegan, touchesMoved in it, and use this imageview subclass to display your images in the scroll view.
On the touchesBegan create a new image view and add it to the outer view and set its image to be the same as the one in the scroll view. You need to overlay it directly over your source image in the scroll view so adjust its frame origin to be relative to the outer view you will need to use the scrollview origin and also the content view size and offset of the source image view inside the content view in order to recalculate the new origin in the outer view.
Then on the touches moved, simply readjust the frame of this image in accordance with the coordinates from the touches moved so that the image follows the touch.
Do a boundary check against the frame of your target imageview - once the user drags it into this boundary, make that target imageviews image the same as the image in the view being dragged and remove the dragged image from the containing view and release it.
In case you're still interested in another solution (and for other users of course):
I did implement that behaviour before just like gamozzii recommended.
I set canCancelContentTouches = NO on the UIScrollView to make sure the subviews handle there touches on their own. If a subview (in your case an image) was touched, i moved the view out of the scrollview onto the superview and started tracking it's dragging. (You have to calculate the correct coordinates within the new superview, so it stays in place). After dragging finishes, i checked if the target area was reached, otherwise I moved it back into the scrollview.
The subviews are handling the dragging on there own via (touchesBegan:/Moved:/Ended:/Cancelled:).
If that's not detailed enough, here's my example code: Github: JDDroppableView

How can I make a UIView that is stretchable by its corners?

I'd like to have a UIView where the user can select each of the four corners and stretch the view by independently moving them.
How would I implement such a view?
To do this, you will need to subclass UIView and handle touch events manually. When you get a touch event, you will have to do some math and then set the frame of the view to the new size. I'd recommend making the background image a stretchable image using stretchableImageWithLeftCap:topCap. It shouldn't actually be that hard.

UIScrollView inside a CALayer

I'm developing some kind of card game in which my view objects (cards, etc) are all CALayers. These objects are added as sublayers of a mainBoard (a CALayer), which, in turn, is a sublayer of the [UIView layer] property of my mainView. Everything is Ok, I can do hit testing to verify which layer is being touched through the touchesBegan:withEvent: callback of the mainView class normally, and so on.
However, I need to have a scroll inside my mainBoard to show "bigger" CALayers, so I tried first adding the "bigger" CALayer inside a CAScrollLayer, but I noticed that the CAScrollLayer doesn't really scroll (doesn't handle user input, neither draws scrollbars). So, the workaround would be to add an UIScrollView directly to the mainView UIView. The UIScrollView scrolls perfectly, until I try to add the "bigger" layer to it, by using [scrollView.layer addSublayer:<bigger layer>].
Does anyone has any idea of how I can have "scrollable" objects inside a CALayer?
Thanks!
nacho4d pointed the answer for my question.
Replying to mark: the "bigger" layer (a layer whose bounds is bigger than the area available in the screen (and so, needs to be contained in a scrollable area)).
The solution was to first wrap the "bigger" layer in a UIView:
UIView *wrapperView = ...
[wrapperView.layer addSublayer:<bigger_layer>];
afterwards, I can add the view as a subview of the UIScrollview:
[<uiscrollview> addSubview:wrapperView];
// set up scrolling properties
<uiscrollview>.contentSize = <bigger_layer>.frame.size;
The problem was that I was trying to add the "bigger" layer as a sublayer of the UIScrollview directly, instead, wrapping it in a UIView and adding it as a subview worked.
Thanks

Shadow inside UIView

I am working on iPhone application development and have come across shadows of UIView.
I know how to show shadows to a UIView but what I am actually interested in is to drop shadow inside the UIView.
Like when I set shadow properties of a UIView the shadow is dropped behind the view. I want it to come over the view so that the view looks as if it is pressed inside.
Example of such view is UITextField with roundedRect styling.
Thanks,
It depends a lot on the final effect you want to achieve.
The easies way would be a custom image with a prebacked shadow as background. This will give the illusion of a recession in the surface of the view. You can then add subviews to it as usual.
Alternatively, you can override the drawRect: method and draw the view as you like there, "inverted drop shadow" included.

iPhone - strange issue with UIScrollView and touches

I have two UIImageView objects inside my view (both 320x480 one above the other). The lower image view is inside a UIScrollView with scrolling and zooming enabled. Now I want to handle touches inside the other image view but it no longer detects any single taps.
I can understand that the UIScrollView handles all the touches which I do on it. But the touches on the image view above the scroll view are also not recognized.
Attached is an image with my view hierarchy. Can someone please tell me why the other image view's touches are also handled by the scrollview when it isn't a subview of scroll view?
And if the scrollview is bent on handling touches, how do I recognize touches on the other image view?
Thanks.
By default UIImageViews have userInteractionEnabled=NO;
Try setting it to YES (either in IB or in code).
I'm not sure, it might be that you need to set the frame size and/or content size of the scroll view properly. This page has a diagram of what I'm talking about.