Overlaying a UIScrollview without cutting off touch events to the scrollview - iphone

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.

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];

Moving UIButton from UIScrollView to UIView in iPhone SDK

I am stuck with a problem actually the scene is like this in my view controller i have place several buttons which i can move within the view now in same view half of the screens is occupied by uiscrollview in this scrollview also i have several uibuttons which i want to move from uiscrollview to uiview Now when i try to move uibutton from uiscrollview to uiview it hides as it moves from the scrollview similarly as i move uibutton from uiview to uiscrollview then also it hides as my drag reaches the scrollview area.
Please help me out with this problem
Thanks in advance....
Your UIButtons each have a superview. For the UIButtons in the scrollview, the UIScrollView is the superview. When you have scrollView.clipsToBounds == YES, then the UIButtons in the scrollview will become obscured if you move them outside of the visible area of the scrollview.
There are several possible solutions, including:
Add code to change the superview of the UIButtons once they reach the edge of the scrollview (but this is tricky, and I wouldn't do it unless there was an easier option, check out Apple's UIView documentation, especially (UIView)removeFromSuperview and (UIView)addSubview:). You would have to perform this switch of superview in the code that moves the button (or tracks the move).
Add the UIButtons to a UIView which is the parent of the scrollview, maybe even your viewcontroller.view (but your UIButtons in the scrollview will no longer move with the scrollview upon scrolling). You would add the buttons to the view behind the scrollview, but so that they show above it.

How to use drawRect with a subclass of UIScrollView?

I'm trying to implement an owner-drawn view that is a subclass of UIScrollView. Basically, I want to custom-draw the contents of the view, and then have the stuff I've drawn be scrollable.
I've overridden drawRect in my view and I'm able to draw my contents, scaled to the size of the UIScrollView's contentSize property (so some of my custom drawing is not visible, as I intended).
The problem is that the content then never moves. I can drag my finger up and down, and this makes the UIScrollView's scrollbars appear, but my custom-drawn content never moves or changes - I still always only see the top half.
How can I custom-draw content for this UIScrollView so that what I've drawn is scrollable?
Are you calling [super drawRect]; at the end of your own drawRect method?
Edit
I misread the question. You'll need to create your own UIView subclass and put your overridden drawRect in that. Then, add that view as a child of the UIScrollView.

how does a subview of a scrollview know the scrollview is being scrolled

I placed a few uiimageview objects into a scrollview. How can the imageview know the scrollview is being scrolled? Since the imageview is a subview of the scrollview i can't set the scrollview delegate to the imageview.
I want to create something similar to the apps view on the iphone. Where you can hold down an app and then drag it, but if you hold and move your finger too far to the left or right the action is stopped and scrolling takes over.
"Since the imageview is a subview of the scrollview i can't set the scrollview delegate to the imageview."
And why not?
You have viewcontroller which shows the particular scrollview correct? this viewcontroller should be the delegate of the scrollview, and viewcontroller should also hold pointers (either explicitly in an array or through tag of some sort) to the uiimageviews.
Then, whenever scrollview notifies viewcontroller (through scrollviewdidscroll type of delegate method), implement your logic to update uiimageview.
To implement the exact touch sequence, you can subclass UIGestureRecognizer or write your own touch handler.. take a look at Apple's documentation on Event Handling Guide :
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541
It doesn't, it just draws and draws. The scrollView makes sure you only see the part you really want to see
You might want to look at three20, they have a class that is very similar to the springboard. -- Used in facebook.app
Thanks Everyone, but I found out that UIScrollView sends the touchesCancelled message to the subview when scrolling starts.

UIScrollView and CALayers

I have a custom UIView that is composed entirely of CALayers.
In the awakeFromNib method it creates and sets all the CALayers into their appropriate positions (CAGradientLayer, several CATextLayers, and a few custom CALayer subclasses). The custom UIView does not override the drawRect: method because there's no drawing done directly into the view (all of the drawing is done in the sublayers).
So I took this view and embedded it in a UIScrollView. The problem? No scroll bars appear and the view does not scroll. The view is clearly larger than the bounds of the scroll view, and instead of allowing me to scroll, it just cuts off at the scroll view bounds.
What could be wrong here?
You have to set the scrollView's contentSize.