0

我的应用程序中有很多字符串,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
4

1 回答 1

2

您根本不必子类化 UITextView,只需使用UISwipeGestureRecognizer. 在您的视图控制器中,您将添加如下内容:

//Updated for both left and right swipes

//Create one gesture recognizer for the swipe left
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionToBePerformedOnSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.textView addGestureRecognizer:swipe];

//Then do the same for UISwipeGestureRecognizerDirectionRight

现在将通知您的视图控制器用户在文本视图上滑动。此外,本教程可能有助于阐明手势识别器。编辑:您可以通过检查来查询手势识别器(在动作方法的 sender 参数中)的方向((UISwipeGestureRecognizer *)sender).direction

但是,如果您想走 UITextView 路线,则必须添加一个使视图控制器成为文本视图的委托并添加一个表示滑动的方法。在您的文本视图子类的标题中,您将添加如下内容:

@protocol SwipeableTextViewDelegate <UITextView>
-(void)textViewReceivedLeftSwipe;
-(void)textViewReceivedRightSwipe;

@end

当收到滑动时,自定义文本视图将在委托上调用这些方法,并且委托(您的视图控制器)将执行您想要的任何操作。

希望这可以帮助!

于 2013-06-25T15:35:26.353 回答