简体   繁体   中英

UITextView Swipe Gesture

I am having a UITextView to display todays report. I have two buttons for going to next day and previous day.

I want to implement swipe gesture such that when the user swipes from left to right, i will display the next day and when swiped from right from left, i will show the previos day.

My problem is swipe gesture is working properly if done outside the UITextField but when i swipe on the UITextField, it is not working.

I see that UITextField implement UIResponder protocol so i believe swipe should work on UITextfield.

Any help?

UISwipeGestureRecognizer *aSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
aSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

UITextView *aTxtView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[aTxtView addGestureRecognizer:aSwipe];
aTxtView.userInteractionEnabled = YES;
aTxtView.backgroundColor = [UIColor redColor];

[self.view addSubview:aTxtView];

[aSwipe release];
[aTxtView release];

This one is for left gesture only. You can do same for right gesture.

Few things, some UI elements provided by apple do not allow you to override the t ouches method , i know this applies for MKMapView, i do not know if it applies to UITextView.

If you actually can override the touch method of a UITextView, are you subclassing the UITextView and overriding the touches method? if not and you are just using a UITextView then the view wont respond to touches because you have not specified it to (by subclassing and coding the touches methods).

I have made a tutorial on this. Have a look at

http://www.jdb-software.com/articles/tutorial-swipetextfield.php

You have to implements the swipe values yourself to display the correct date instead of the colors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM