我的应用程序中有很多字符串,NSArray
用户通常可以单击与 ib 操作链接的按钮来进行下一步或上一步。当他们单击下一个或上一个时,a 中的文本TextView
将更改为数组中的下一个或上一个字符串。我希望用户能够TextView
在下一个或上一个中滑动。我已经了解了一些关于如何识别滑动的知识,但这需要一个全新class
的继承UITextView
,而我的另一个包含数组和 ib 动作的类继承了一个UIViewcontroller
. 我将发布我的代码的样子,我只想知道如何连接滑动类或在动作中识别滑动。谢谢你的时间!
//DateIdeasViewController.m
#import "DateIdeasViewController.h"
@interface DateIdeasViewController ()
@end
@implementation DateIdeasViewController
@synthesize labelsText;
@synthesize textView;
@synthesize adView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) bannerViewDidLoadAd:(ADBannerView *)banner {
[adView setHidden:NO];
NSLog(@"Showing");
}
- (void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[adView setHidden:YES];
NSLog(@"Hidden");
}
-(void)viewDidLoad {
adView.delegate = self;
[adView setHidden:YES];
titles = [NSArray arrayWithObjects:
//Date ideas
@"Some date ideas may be seasonal!",
nil];
step= 0;
textView.text = [titles objectAtIndex:step];
labelsText.text = [NSString stringWithFormat:@"%d/%d", step+1, titles.count];
}
-(IBAction) nextclicked:(id)sender{
if (step<titles.count-1) {
step++;
}
else
{
step= 0;
}
textView.text = [titles objectAtIndex:step];
labelsText.text = [NSString stringWithFormat:@"%d/%d", step+1, titles.count];
}
-(IBAction) prevClicked:(id)sender{
if (step>0) {
step--;
}
else
{
step =titles.count-1;
}
textView.text = [titles objectAtIndex:step];
labelsText.text = [NSString stringWithFormat:@"%d/%d", step+1, titles.count];
}
-(IBAction) randomClicked:(id)sender{
step = 1+arc4random() %(titles.count-1);
textView.text = [titles objectAtIndex:step];
labelsText.text = [NSString stringWithFormat:@"%d/%d", step+1, titles.count];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction) favorite:(id)sender{
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"]];
[array addObject:textView.text];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"favorites"];
}
@end
SwipeableTextView.h
#import <UIKit/UIKit.h>
#define kMinimumGestureLength 25
#define kMaximumVariance 5
typedef enum swipeDirection {
kSwipeNone,
kSwipeLeft,
kSwipeRight
} tSwipeDirection;
@interface SwipeableTextView : UITextView {
CGPoint gestureStartPoint;
tSwipeDirection swipeDirection;
}
@end
SwipeableTextView.m
#import "SwipeableTextView.h"
@implementation SwipeableTextView
- (id)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
swipeDirection = kSwipeNone;
UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
// Check if we already started a swipe in a particular direction
// Don't let the user reverse once they get going
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance &&
swipeDirection == kSwipeNone) {
if (gestureStartPoint.x < currentPosition.x) {
swipeDirection = kSwipeRight;
}
else {
swipeDirection = kSwipeLeft;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (swipeDirection == kSwipeRight) {
}
else if (swipeDirection == kSwipeLeft) {
NSLog(@"Swipe left");
}
[super touchesEnded:touches withEvent:event];
}
@end