Can't remove imageView from within UIGestureRecognizer? - iphone

I have a photo on the screen which when held, I want to display at full size. Here's the gesture recognizer:
UILongPressGestureRecognizer *hold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(hold:)];
hold.minimumPressDuration = 0;
[self.photoImageView addGestureRecognizer:hold];
And here's the listener:
-(void)hold:(UILongPressGestureRecognizer *)sender{
UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.userInteractionEnabled = NO;
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(#"state began");
[self.view addSubview:img];
} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(#"state ended");
[img removeFromSuperview];
}
}
The image gets added to the screen correctly, however [img removeFromSuperview] does not seem to respond. Any reason for this happening? The NSLog for ending state is correctly firing off.

It's because you are creating a new instance of UIImageView instead of removing the one that is displayed.
-(void)hold:(UILongPressGestureRecognizer *)sender{
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(#"state began");
UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.tag = IMAGE_VIEW_TAG;
img.userInteractionEnabled = NO;
[self.view addSubview:img];
} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(#"state ended");
UIImageView *img = [self.view viewWithTag: IMAGE_VIEW_TAG];
[img removeFromSuperview];
}
}

You don't need to create new imageview in gesture's action. Just add or remove your self.photoImageView like this
-(void)hold:(UILongPressGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
NSLog(#"state began");
[self.view addSubview:self.photoImageView];
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
NSLog(#"state ended");
[self.photoImageView removeFromSuperview];
}
}

Related

Multiple gestureRecognizers on views- one shouldn't pass touch

On parentClass(inheriting from UIView) i have:
[self addGestureRecognizer:_tapGesture]; // _tapGesture is UITapGestureRecognizer, with delegate on parentClass
On someClass:
[_myImageView addGestureRecognizer:_imageViewGestureRecognizer]; // _imageViewGestureRecognizer is UITapGestureRecognizer, with delegate on someClass
The problem is that when i tap on myImageView both gesture recognizers are firing. I want only _imageViewGestureRecognizer to work.
I've tried:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {
UIView *gestureView = recognizer.view;
CGPoint point = [touch locationInView:gestureView];
UIView *touchedView = [gestureView hitTest:point withEvent:nil];
if ([touchedView isEqual:_imageViewGestureRecognizer]) {
return NO;
}
return YES;
}
But it ofc doesn't take upon consideration gesture recognizer from super class.
I did this little test and it worked perfectly...
#implementation View
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped1)];
[self addGestureRecognizer:tap];
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
img.userInteractionEnabled = YES;
img.frame = CGRectMake(0, 0, 100, 100);
[self addSubview:img];
UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped2)];
[img addGestureRecognizer:tap2];
return self;
}
-(void)tapped1 {
NSLog(#"Tapped 1");
}
-(void)tapped2 {
NSLog(#"Tapped 2");
}
#end
What you wanted was iOS's default behaviour. Once a subview has taken care of a touch, its superview won't receive the touch anymore. Did you set userInteractionEnabled on the imageView?

When Im moving view picked from cloudScrollView not showing the picked view in front

Adding UIImageView on UIScrollView and adding UIPanGestureRecognizer to it
UIImageView* lbl = [[UIImageView alloc]init];
NSString* tempStr = [NSString stringWithFormat:#"images/%#",[self.cloudArray objectAtIndex:i]];
NSString* imgPath = [self appendDocumentDirectoryPath:tempStr];
lbl.image = [[[UIImage alloc] initWithContentsOfFile:imgPath] autorelease];
[lbl setUserInteractionEnabled:YES];
//add gestures
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[lbl addGestureRecognizer:panGesture];
[panGesture release];
CGSize expectedLabelSize = CGSizeMake(150, height);
lbl.frame = CGRectMake(pntX, pntY, expectedLabelSize.width, height);
[rectFrame addObject:[NSValue valueWithCGRect:lbl.frame]];
lbl.tag = i;
[cloudScrollView addSubview:lbl];
[lbl release];
lbl = nil;
pntX = pntX + space + expectedLabelSize.width;
[cloudScrollView setContentSize:CGSizeMake([cloudArray count]*170, 160)];
#pragma mark METHOD TO CATCH MOVE GESTURE EVENTS
- (void)moveImage:(UIPanGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) {
location1 = [gesture locationInView:cloudScrollView];
view1 = [gesture view];
if (isFirstTime) {
isFirstTime = NO;
draggedTag = view1.tag;
}
[view1 setCenter:CGPointMake(location1.x, location1.y)];
}
if (gesture.state == UIGestureRecognizerStateEnded) {
CGPoint pnt = [gesture locationInView:customTableView];
NSIndexPath* indexPath = [customTableView indexPathForRowAtPoint:pnt];
NSLog(#"indexpath = %d",indexPath.row);
if (index >= 0) {
[self.ansArray replaceObjectAtIndex:indexPath.row withObject:[self.cloudArray objectAtIndex:draggedTag]];//[self.cloudArray objectAtIndex:draggedTag]
[customTableView reloadData];
}
CGRect draggedItemRect = [[rectFrame objectAtIndex:draggedTag] CGRectValue];
view1.frame = draggedItemRect;
isFirstTime = YES;
}
}

How set a keyboard for number text field and alphabetic text field?

I have a layout where i have two text fields one is for alphabetic input and another one for numeric input. I have set different type of keyboards for these like as default for alphabetic text field and number for numeric field.
I have added a done button on number pad. Still I am getting this a issue with keyboards that is when I taped on numeric text filed first then it show done button keyboard while I taped alphabetic text filed first and then taped on numeric text field then it doesn't add done button on number pad.
How do I solve it?
I have use these code to add button on number pad.
-(void)viewWillAppear:(BOOL)animated
{
// Register for the events
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
keyboardVisible = NO;
scroll_view.contentSize=CGSizeMake(320, 400);
}
- (void)keyboardDidShow:(NSNotification *)note {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for (keyboard in tempWindow.subviews) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
if (numberPadShowing) {
[self addButtonToKeyboard];
return;
break;
} else {
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
-
(void) keyboardDidHide: (NSNotification *)notif
{
// Is the keyboard already shown
if (!keyboardVisible) {
return;
}
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(void)doneButton
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[myActiveTextField resignFirstResponder];
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(doneButton) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
myActiveTextField=textField;
if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){
numberPadShowing=TRUE;
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
svos = scrollView.contentOffset;
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrollView];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scrollView setContentOffset:pt animated:YES];
}
else{
numberPadShowing=FALSE;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[textField resignFirstResponder];
return YES;
}
Thanks in advances...
I think your problem is becuase you are tapping in to the numeric field after tapping the alpha field, and the alpha keyboard is already in view. Therefore when you tap the number field (whilst alpha keyboard still in view) it doesn't fire the 'keyboardDidShow' event, as the keyboard is already showing from the previous field (albeit a alpha one).
Try putting your done button logic into a delegate method for the textfield 'textFieldDidBeginEditing' event for the number field.
If my first answer doesn't work in your implementation, then you might want to consider a restructure and go for setting the inputAccessoryView of your UITextField. Code along these lines should do the trick (you can put this in your viewDidLoad nib, and also note that you'll need to implement the doneTapped method yourself);
// Create a UIToolbar object and set its style to be black and transparent
numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent;
// The flexible space item will push the done button to the far right
UIBarButtonItem *space = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself.
UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTapped:)];
// Stick the flexible space and the done button in your toolbar
numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil];
// This would return it, if you had this in a separate method.
return numericInputAccessoryView_;

How to use UISwipeGestureRecognizer on UIButton?

I have a UIbutton which i want to work as a joystick. So i am trying to add some gesture recognizer on the same button.
I have this in my code right now:
#implementation CUETutorialSixteenClusterRootController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:#"CUETutorialLandscapeClusterRootController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)init {
self = [super init];
if (self) {
_leftInActiveView = [[NSMutableArray alloc] init];
_rightInActiveViews = [[NSMutableArray alloc] init];
_centerInActiveViews = [[NSMutableArray alloc]init];
_centerActiveViews = [[NSMutableArray alloc]init];
UIImageView * inActLeftView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActLeftView.image = [UIImage imageNamed:#"1-am-station_01.png"];
[_leftInActiveView addObject:inActLeftView];
UIImageView * inActRightView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActRightView.image = [UIImage imageNamed:#"1-am-station_03.png"];
[_rightInActiveViews addObject:inActRightView];
NSArray * imagesName = [NSArray arrayWithObjects:[UIImage imageNamed:#"1_start.png"],
[UIImage imageNamed:#"2_apptray_audio_selected.png"],
[UIImage imageNamed:#"2_apptray_phone_selected.png"],
[UIImage imageNamed:#"4_phonemenu_contacts_highlighted.png"],
[UIImage imageNamed:#"5_phonemenu_recentcalls_highlighted.png"],
[UIImage imageNamed:#"6_recent_calls.png"],
[UIImage imageNamed:#"7_bottom_hit.png"],
[UIImage imageNamed:#"8_recent_call_details.png"],
[UIImage imageNamed:#"9_calling.png"],
[UIImage imageNamed:#"9_calling_myphone.png"],
[UIImage imageNamed:#"9_call_ended.png"],nil];
NSLog(#"Count:%d",[imagesName count]);
for (int i =0; i<11; i++) {
UIImageView * inActCenterView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 226, 98)] autorelease];
inActCenterView.image = [imagesName objectAtIndex:i];
[_centerActiveViews addObject:inActCenterView];
[_centerInActiveViews addObject:inActCenterView];
}
}
return self;
}
-(void)showClusterType:(clusterType)cluster{
switch (cluster) {
//Base Cluster is analog
case kClusterTypeBase:
{
[self.fullClusterContainerView removeFromSuperview];
[self.view addSubview:self.baseClusterContainerView];
[self.view addSubview:self.baseClusterImageView];
CUETutorialSixteenAplicationHomeScreen * clusAppScreen = [[CUETutorialSixteenAplicationHomeScreen alloc] initWithDelegate:self];
clusAppScreen.dataSource = self;
clusAppScreen.rootController = self;
self.clusterHomeScreen = clusAppScreen;
[self.baseClusterContainerView addSubview:clusAppScreen.view];
[clusAppScreen release];
/*
[self.view bringSubviewToFront:self.steeringWheelImageView];
[self.view bringSubviewToFront:self.promptLabel];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavDownButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavUpButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonUp]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonLeft]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonRight]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonDown]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonCenter]];
*/
[self.view bringSubviewToFront:self.promptLabel];
}
break;
//Full cluster is digital
case kClusterTypeFull:
{
[self.baseClusterImageView removeFromSuperview];
[self.baseClusterContainerView removeFromSuperview];
[self.view addSubview:self.fullClusterContainerView];
}
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
_step =1;
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
time=0;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc{
[super dealloc];
[_leftInActiveView release];
[_rightInActiveViews release];
[_centerActiveViews release];
[_centerInActiveViews release];
}
-(void)animateToView:(UIView *)aview{
if(aview){
//CGRect rect = [aview convertRect:aview.bounds toView:self.view];
CGRect rect = aview.frame;
[self animateHandFromRect:self.promptLabel.frame toRect:rect];
}
else{
[self animateHandToNSValueRect:nil];
}
}
-(void)finishTutorial{
[self displayPromptText:#"TUTORIAL COMPLETE"];
[self displayNavigationBarWithTitle:#"TUTORIAL COMPLETE"];
[self performBlock:^{
[self.clusterHomeScreen.view removeFromSuperview];
[self showScoresScreen];
}afterDelay:1];
}
-(void)animateHandToCenterButton
{
[self displayPromptText:#"TAP RIGHT SIDE CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE CENTER BUTTON"];
[self animateToView:self.rCenterButton];
}
-(void)animateHandToDownButton {
[self displayPromptText:#"TAP RIGHT SIDE DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE DOWN BUTTON"];
[self animateToView:self.rDownButton];
}
-(void)animateHandToUpButton{
[self displayPromptText:#"TAP RIGHT SIDE UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE UP BUTTON"];
[self animateToView:self.rUpButton];
}
-(void)replaceCallScreen{
[self performBlock:^{
[self pressedRightButtonCenter];
} afterDelay:2.0];
}
-(void)changeTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(startTicking) userInfo:nil repeats:YES];
}
-(void)startTicking {
time = time +1;
self.clusterHomeScreen.callTimer.text = [NSString stringWithFormat:#"00:0%d",time];
if (time==9) {
[timer invalidate];
}
}
-(void) animateHandToVolUpButton {
[self displayPromptText:#"TAP VOLUME UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME UP BUTTON"];
[self animateToView:self.rVolUpButton];
}
-(void) animateHandToVolDownButton {
[self displayPromptText:#"TAP VOLUME DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME DOWN BUTTON"];
[self animateToView:self.rVolDownButton];
}
-(void)removeVolumeView {
[self pressedRightButtonCenter];
[self.clusterHomeScreen runNextStep];
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.%#",recognizer);
}
-(IBAction)buttonClicked:(id)sender
{
if ([sender tag]==kRightButtonDown && _step ==1)
{
[self pressedRightButtonDown];
[self displayPromptText:#"TAP AGAIN"];
[self displayNavigationBarWithTitle:#"TAP AGAIN"];
_step++;
}
else if ([sender tag]==kRightButtonDown && _step == 2)
{
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step == 3)
{
[self pressedRightButtonCenter];
[self animateToView:self.rUpButton];
_step++;
}
else if ([sender tag]==kRightButtonUp && _step == 4)
{
[self pressedRightButtonUp];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
[self animateToView:self.rCenterButton];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==5){
[self pressedRightButtonCenter];
[self displayPromptText:#"TAP DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP DOWN BUTTON"];
[self animateToView:self.rDownButton];
_step++;
}
else if([sender tag]==kRightButtonDown && _step==6){
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==7){
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==8)
{
[self pressedRightButtonCenter];
self.clusterHomeScreen.callTimer.hidden = NO;
[self.clusterHomeScreen runNextStep];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==9)
{
self.clusterHomeScreen.callTimer.hidden = YES;
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==10)
{
[self pressedRightButtonCenter];
[self finishTutorial];
}
else {
[self displayVisualFeedback:kVisualFeedBackIncorrectAnswer];
}
}
#pragma CUEClusterHomeSreenDataSource Methods
-(NSInteger)numberOfViewsForLeftScreen {
return [_leftActiveViews count];
}
-(NSInteger)numberOfViewsForRightScreen{
return [_rightActiveViews count];
}
-(NSInteger)numberOfViewsForCenterScreen {
return [_centerActiveViews count];
}
-(UIImageView *)inActiveViewForLeftScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_leftInActiveView objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForLeftScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_leftActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightInActiveViews count]) && (index >=0)){
return [_rightInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightActiveViews count]) && (index >=0)){
return [_rightActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForCenterScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_centerInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForCenterScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_centerActiveViews objectAtIndex:index];
//}
//return nil;
}
This is the error i am getting now:
-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0
2012-03-28 13:25:55.724 CUETrainer[1788:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0'
But its not actually doing anything.
Please help!!!!
The error says unrecognized selector sent to instance 0x79b71b0' most probably the names in .h and .m are different..
The target of the gesture recognizer needs to be the object that implements the selector. What's happening now when a swipe happens is that the gesture recognizer is doing [self.gestureReceiverButton handleSwipeFrom:recognizer]
You probably want this:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
Change every instance of:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
to
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
Problem solved. Be very careful with your #selectors, what you place in this macro is NOT validated by the compiler. Code complete will help you with this.
Also you are adding four gesture recognizers to one view, while you can accomplish the same task with one. You can combine the valid directions using the bitwise OR operator, like so:
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown)];

Find and Remove Certain one UIGestureRecognizer among some same named UIGestureRecognizers

Hey, guys, I met a problem when trying to removeGestureRecognizer: from a view,
what i want to do is doubleTap one of the imageViews, and remove the tapped imageView's singleTap Gesture, without remove other imageViews singleTap Gesture.
here is how i generate views, gestures and the mechanisms:
.h
UITapGestureRecognizer *singleTap;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger i;
for (i = 1; i <= 3; i++)
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(110, 70+80*(i-1), 100, 60);
imageView.backgroundColor = [UIColor whiteColor];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleMethod:)];
doubleTap.numberOfTapsRequired = 2;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleMethod:)];
[singleTap requireGestureRecognizerToFail:doubleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:singleTap];
}
}
- (void)singleMethod: (id)sender
{
NSLog(#"SingleTap");
}
- (void)doubleMethod: (id)sender
{
NSLog(#"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);
UIImageView *imageView = nil;
NSArray *tryToFindYou = [self.view subviews];
for (imageView in tryToFindYou)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag)
{
[imageView removeGestureRecognizer:singleTap];
}
}
NSLog(#"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);
}
but these lines I wrote can't find exactly the singleTap Gesture attached to the double-tapped imageView.
when NSLog the .gestureRecognizers count, it still 2, what it removed is the last imageView's singleTap Gesture, it became 1, which is correct.
I can't locate the first and second one, any ideas to locate them? thank you for reading :)
You should cycle through gestureRecognizers property of the UIView class, where the gestures are added, something like this:
for (imageView in tryToFindYou)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag)
{
for(UIGestureRecognizer *gesture in [imageView gestureRecognizers])
{
if([gesture isKindOfClass:[UITapGestureRecognizer class]])
{
if (gesture.numberOfTapsRequired == 1)
[imageView removeGestureRecognizer:gesture];
}
}
}
}