如何在一个字符串数组中的视图控制器内滑动?

时间:2013-06-25 14:35:03

标签: ios uitextview swipe

我的应用在NSArray中有很多字符串,用户通常可以点击与ib操作相关联的按钮,以便下一个或上一个。当他们点击下一个或上一个时,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

1 个答案:

答案 0 :(得分: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

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

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

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

@end

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

希望这有帮助!