Is there a way to play through a sequence of images using the Pinch Gesture tool in XCode? If so, how?
Any help will be greatly appreciated! :)
Dave.
this is the common solution , by adding pinchGesture in every imageView and this calls the callback delegate , when ever the gesture event starts.
for (int i = 0;i<=8;i++){
UIImageView *imageView = [[UIImageView alloc] init]
imageView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinch:)];
pinch.delegate = self;
pinch.tag = self;
[imageView addGestureRecognizer:pinch];
}
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
//handle pinch...
}
Related
I'm trying to implement a zoom feature for an iCarousel item view. This is my code :
NSString *pathToHDImage = [documentsFolderPath stringByAppendingPathComponent:solution.localHDURL];
if (!view) {
view = [[AsyncImageView alloc] initWithFrame:self.view.bounds];
view.contentMode = UIViewContentModeScaleAspectFit;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchGesture.cancelsTouchesInView = false;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panGesture.cancelsTouchesInView = false;
pinchGesture.delegate = panGesture.delegate = self;
[view addGestureRecognizer:pinchGesture];
[view addGestureRecognizer:panGesture];
((AsyncImageView *)view).imageURL = [NSURL fileURLWithPath:pathToHDImage];
} else
((AsyncImageView *)view).imageURL = [NSURL fileURLWithPath:pathToHDImage];
The problem is that my handlePan: and handlePinch: never get called. How can I do? The main goal is to implement zoom feature for every iCarousel item view.
Try this Code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
I did not test but I hope it helps.
In case it does not work check out this link1 and then this link2
I have an object based on UIView that I've set up to recognise swipe gestures. The view has another UIView based object in the middle of it.
When I swipe left or right, regardless of where I put my finger, the correct event fires.
When I swipe up or down the correct event only fires if my finger isn't starting in the subview in the middle?
Why?
Here's the init code for the object:
- (id)init
{
self = [super init];
if (self) {
[self setFrame:CGRectMake(0,
0,
[self getScreenWidth],
[self getScreenHeight])];
backView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
[self getScreenWidth],
[self getScreenHeight])];
backView.backgroundColor = [UIColor whiteColor];
[self addSubview:backView];
[self bringSubviewToFront:backView];
theCard = [[actCard alloc] init:CARD_POS_CENTER];
[backView addSubview:theCard];
[backView bringSubviewToFront:theCard];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(showGestureForSwipeRecognizer:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipeLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[self addGestureRecognizer:swipeUp];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[self addGestureRecognizer:swipeDown];
NSLog(#"Enabled: %d",swipeLeft.isEnabled);
NSLog(#"Enabled: %d",swipeRight.isEnabled);
NSLog(#"Enabled: %d",swipeUp.isEnabled);
NSLog(#"Enabled: %d",swipeDown.isEnabled);
}
return self;
}
Try setting the delegate of your Upward swipe and downward swipe to be your MainViewController.Something like :
swipeDown .delegate = self;
swipeUp .delegate = self;
This should make your main view controller ( which contains the secondary UIView ) responsible for the gestures received.
Hope this helps!
Obvious! What a newb I am.
The UITextView still had scrollEnabled. That means the UITextView took the up & down swipes, they don't get passed on to the view.
This question already has answers here:
exclude subview from UITapGestureRecognizer
(3 answers)
Closed 9 years ago.
This is how I add gesture on view
- (void)_addPanGestureToView:(UIView *)view {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(_handlePan:)];
panGesture.delegate = self;
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
[view addGestureRecognizer:panGesture];
}
Everything is working perfectly, but gesture is on the whole view how could I do something like gesture respond only in half of view?
Why not just use CGRectContainsPoint() and check if the touches location within your view is within the area you want it to be. If it isn't, ignore it:
- (void)panGestureDetected:(UIPanGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:sender.view];
CGRect someRect = ...
if (CGRectContainsPoint(someRect, location)) {
// point is in specified area
}
}
Easiest solution would be adding a transparent view on the area where you want you gesture recognizer to work, and add the gesture to that view (and that view as a subview of course).
Somthing like:
- (void)_addPanGestureToView:(UIView *)view {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(_handlePan:)];
panGesture.delegate = self;
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
UIView *viewForGesture = [[UIView alloc] initWithFrame:CGRectMake(....)]; //your frame
[viewForGesture addGestureRecognizer:panGesture];
[view addSubview:viewForGesture];
}
I have a UIScrollView and inside this I have UILabels. I need to detect touch events for the UILabels. At the moment, it is detecting the touch inside the second label only. It ignores the first.
I have the code -
Creating the UIScrollView
backGroundView = [[UIScrollView alloc] init];
backGroundView.frame= self.view.frame;
backGroundView.userInteractionEnabled = YES;
[backGroundView setScrollEnabled:YES];
backGroundView.showsVerticalScrollIndicator = YES;
backGroundView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
backGroundView.delegate = self;
[self.view addSubview:backGroundView];
Creating the UILabel
UILabel *OneDay = [[UILabel alloc] initWithFrame:CGRectMake(15, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
OneDay.text = #"1d";
OneDay.tag = 1;
OneDay.userInteractionEnabled = YES;
OneDay.layer.borderColor = [UIColor grayColor].CGColor;
OneDay.layer.borderWidth = 1.0f;
OneDay.textAlignment = UITextAlignmentCenter;
[OneDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:OneDay];
UILabel *FiveDay = [[UILabel alloc] initWithFrame:CGRectMake(45, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
FiveDay.text = #"5d";
FiveDay.tag = 2;
FiveDay.userInteractionEnabled = YES;
FiveDay.layer.borderColor = [UIColor grayColor].CGColor;
FiveDay.layer.borderWidth = 1.0f;
FiveDay.textAlignment = UITextAlignmentCenter;
[FiveDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:FiveDay];
Creating the gesturerecognizer
UITapGestureRecognizer *detectTimeFrameChange = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(timeFrameLabelTapped:)];
detectTimeFrameChange.numberOfTapsRequired = 1;
[backGroundView addGestureRecognizer:detectTimeFrameChange];
Handling gesture
-(void)timeFrameLabelTapped:(UITapGestureRecognizer*)recognizer{
if (recognizer.view.tag == 1) {
NSLog(#"One pressed");
}
else if (recognizer.view.tag == 2){
NSLog(#"2 pressed");
}
}
You can use this :
UITapGestureRecognizer *labelTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(labelTapped)];
labelTap.numberOfTapsRequired=1;
[yourLabel addGestureRecognizer:labelTap];
handle the touch tap event inside labelTapped method:
-(void)labelTapped
{
//your code to handle tap
}
Touch events are not detected on UIScrollview for getting your requirement add tap gestures to your label.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured)];
[OneDay addGestureRecognizer:singleTap];
-(void)singleTapGestureCaptured{
NSLog(#"touch detected");
}
You can find using tapgesturerecogniser like that...
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.scrollview addGestureRecognizer:singleFingerTap];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if(recognizer.view.tag == 1){}
//Do stuff here...
}
Where have you written the touchesBegan?
If you want to detect the touches in the label you'll have to create a subclass of label and write your touchesBegan there to detect the touch events
The problem here is that you are trying to use the same gesture recognizer for multiple views. A gesture recognizer can only be attached to a single view at once. You are only receiving events from the last view, because that is the view the recognizer is currently attached to. To fix the issue, simply create a gesture recognizer for each view you want to detect touches in.
I have created a UIViewController, which contains a UIView object.
I want the UIView object response to a single tap by 1 finger, and also pinch by 2 fingers.
I have clicked the "User Interaction Enabled" and the "Multiple touch" options in the xib.
I create both UITapGestureRecognizer, and UIPinchGestureRecognizer inside the UIView function initWithFrame:, as it is the only entry point for the UIView object. But the object doesn't response to the UIGestureRecognizer objects. Why? Or, where is the best place to put the UIGestureRecognizer objects?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Your problem is that when you instantiate your custom view subclass in IB, initWithCoder: is called. If you had done it programmatically using initWithFrame: it would have worked properly.
Two ways you can fix this,
Move the gestures setup to a different method and call them in both initWithFrame: and initWithCoder:. I would recommend doing this if you intend to reuse this component and expose some kind of callbacks on gestures.
If you want to implement this once and have a lot of interacting with the controller element, add them in viewDidLoad.
I tried your code and it works for me.
Where are you setting the view? Maybe it has any other view in front, or its superview has userInteractionEnabled disabled.
I created a UIView subclass and added this code:
-(void) handleSingleTap:(UITapGestureRecognizer *)gr {
NSLog(#"handleSingleTap");
}
-(void) handleDoubleMove:(UIPinchGestureRecognizer *)gr {
NSLog(#"handleDoubleMove");
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Then, in my controller:
-(void) viewDidLoad {
[super viewDidLoad];
MyView * myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myView.backgroundColor = [UIColor greenColor];
[self.view addSubview:myView];
[myView release];
}
And it works.
i think if you are working with .xib, initialization can also be done in
-(void)awakeFormNib;
Adding the GestureRecognizers is typically done in the viewDidLoad method of the UIViewController.