I have work on touch event but that would be not work because of I take imageView in ScrollView so,that when I touch the image that directly work scrollview but not work touch on image so give any suggestion and source code which is apply in my code.....
As frauen1 says in their answer here:
1) In the UIScrollView class, set the value of canCancelContentTouches
to NO - this tells the UIScrollView class to allow touches within
subviews (or, in this case, in subviews of subviews).
2) In my "card" class, set exclusiveTouch to YES - this tells the
subview it owns the touches inside of it.
Now it will allow double tap on ImageView with following code
UITapGestureRecognizer *tapRecognizer;
self.imgViewGVC.userInteractionEnabled = YES;
tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTapView:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.ImageView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
Related
I currently have a custom class that implements UIScrollView.
I created one UIImageView and add it to a subview of the UIScrollView. It scrolls vertically and horizontally perfectly, but it seems to be ignoring the zooming stuff. Here is my code:
- (void) setUpBoard
{
// add board
UIImageView *imageHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"grid.jpg"]];
[self addSubview:imageHolder];
self.contentSize = CGSizeMake(imageHolder.frame.size.width, imageHolder.frame.size.height);
self.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.maximumZoomScale = 5.0;
self.minimumZoomScale = 0.2;
self.zoomScale = 0.7;
self.clipsToBounds = YES;
}
How do I get it to zoom using standard pinch gestures? Any help is appreciated. Thanks!
From the UIScrollView Class Reference Overview:
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.
I suspect that either you haven't assigned a delegate to the scroll view, or the delegate doesn't implement both of those methods.
I've put a UIButton inside a custom UIView and the button is not receiving any touch events (it doesn't get into the highlighted state, so my problem is not about being unable to wire up a touch inside up handler). I've tried both putting it into the XIB in Interface Builder, and also tried programatically adding the UIButton into the UIView seperately, both ended with no luck. All my views are inside a UIScrollView, so I first though UIScrollView may be blocking them, so I've also added a button programatically exactly the same way I add my custom view into UIScrollView, and the button worked, elimination the possibility of UIScrollView could be the cause. My View's hiearchy is like this:
The button is over the image view, and the front layer isn't occupying my button completely, so there's no reason for me not be physically interacting with the button. At my custom view's code side, I'm creating my view as such:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIView *sub = [[[NSBundle mainBundle] loadNibNamed:#"ProfileView" owner:self options:nil] objectAtIndex:0];
[self addSubview:sub];
[sub setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
CALayer *layer = sub.layer;
layer.masksToBounds = YES;
layer.borderWidth = 5.0;
layer.borderColor = [UIColor whiteColor].CGColor;
layer.cornerRadius = 30.0;
/*layer.shadowOffset = CGSizeZero;
layer.shadowRadius = 20.0;
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOpacity = 0.8;
*/
}
return self;
}
I've tried all combinations of setUserInteractionsEnabled, and had no luck. (Yes, also set them to checked in Interface Builder too). I've also read in another question with a similar problem that I should try overriding 'canBecomeFirstResponder' to return 'YES' and I've also done that too. But the problem persists, I can't click the button. I've not given any special properties, settings to the button, it's just a regular one. My other objects in the view (labels below, image view behind the button etc.) are working properly without problems. What could be possibly wrong here?
Thanks,
Can.
UPDATE: Here is a quick reproduction of the problem: https://dl.dropbox.com/u/79632924/Test.zip
Try to run and click the button.
Looking at the test project, I believe your problem in the way you create TestView, you do not specify the frame for it, so basically the parent view is 0 size, and the subviews you see from XIB extending out of the parent view and thus do not get anything in responder chain.
You should either specify the frame when creating TestView, or adjust the frame after loading XIB file.
I have had this problem as well. The cause for me was that the UIButton superview frame was of height 0, so I believe that even though a touch was happening, it was not being passed down to the button.
After making sure that the button's superview took a larger rectangle as a frame the button actions worked.
The root cause for this problem on my side was a faulty auto layout implementation (I forgot to set the height constraint for the button's superview).
I've found the solution. I was initializing my custom view as:
MyView *view = [[MyView alloc] init];
I've initialized it instead with a frame of my view's size, and it started responding to events:
CGRect rect = CGRectMake(0,0,width,height);
MyView *view = [[MyView alloc] initWithFrame:rect];
Storyboard Solution
Just for anyone wanting a solution to this when using storyboards and constraints.
Add a constraint between the superview (containing the button) and the UIButton with an equal heights constraint.
In my case, I had selected embed UIButton in a UIView with no inset on the storyboard. Adding the additional height constraint between the UIButton and the superview allowed the UIButton to respond to touches.
You can confirm the issue by starting the View Debugger and visually confirm that the superview of the UIButton is not selectable.
(Xcode 11, *- Should also work in earlier versions)
I have an "PDF Viewer" in my application. I use the CGPDFDocumentRef and CGPDFPageRef to show the PDF. To zoom in the PDF I use a UIScrollView.
I am already able to pinch-zoom in the UIScrollView but I also want to be able to double-tap to zoom.
Double tap with one finger takes you to different levels in the PDF until you hit the maximumZoomLevel and then be able to double-tap with two fingers to the minimumZoomLevel.
I haven't been able to find a solution on the Internet that shows you this in particular so I'm asking here. How could I accomplish this?
In viewDidLoad:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.scrollView addGestureRecognizer:doubleTap];
[doubleTap release];
Method:
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
if(self.scrollView.zoomScale > self.scrollView.minimumZoomScale)
[self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
else
[self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES];
}
I think that you can use UITapGestureRecognizer with numberOfTapsRequired property after that in handle method (which you specify when alloc/init the recognizer) and use to use – zoomToRect:animated: or – setZoomScale:animated: on the view to zoom.
Edit: keep track on current zoom level to reverse the effect.
In my app there is a table view and it have some image view in it .Actually 4 image view in a row.Is there any touch event for the table view so that i am able to select any image view inside the table view and then do some functionalities with it.Please help.Any help will be appreciated.
Thanks,
Christy
You can apply a gesturerecognizer to each of the subviews. Once a gesture of that certain recognizer was detected (tab, swipe, pinch, pan, swipe, rotation, longtab or custom (by subclassing)), a targets selector will be called.
for(UIImageView *imgView in imageViews){
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTapFrom:)];
[imgView addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
}
So, I iterate through a loop and create UIViews which contain UIImageViews (So that I can selectively show any given part). These UIViews are all stored in a UIScrollView.
I add gesture recognizers to the UIViews in the loop where I created them.
When I run the program, only the items initially visible within the UIScrollView have their gestures recognized. If I scroll over to previously hidden items and then tap on them, nothing happens at all (the gesture is never recognized or attempted to be).
Initialization code:
UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
gestRec.delegate = self;
[imageholder addGestureRecognizer:gestRec];
Code that deals with the gesture:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
float count = [self._imageHolders count];
NSLog(#"handling gesture: %f",count);
while(count--){
UIView* object = (UIView*) [self._imageHolders objectAtIndex:count];
// NSLog(#"Whats going on: %#, %#, %b",object,gestureRecognizer.view, object == gestureRecognizer.view);
if(object == gestureRecognizer.view){
object.alpha = .1;
count = 0;
}
// [object release];
}
}
Any ideas?
---- Update :
I've explored a variety of the available functions in scrollview, UIView and the gesture recognizer and have tried messing with the bounds in case something was cut off that way... Interestingly enough, if there is one item only partially visible and you move it over so it's completely visible, only the portion originally visible will recognize any gestures.
I don't know enough about how the gesture recognizer works within the UIKit architecture to understand this problem. The Apple example for a scrollview with gestures doesn't seem to have this problem, but I can't find any real differences, except that I am nesting my UIImageViews within their own UIViews
I had a similar problem and found it was caused by adding the sub-views to a top-level view then adding that top-level view to the scroll-view. The top-level view had to be sized to the same dimensions as the contentSize (not the bounds) of the scroll-view otherwise it wouldn't pass on touch events to its subviews even when they had scrolled into view.
Try to set the cancelsTouchesInView property to NO.
UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
gestRec.delegate = self;
**gestRec.cancelsTouchesInView = NO;**
[imageholder addGestureRecognizer:gestRec];