gpt4 book ai didi

ios - 如何在字符串数组中的 View Controller 内滑动?

转载 作者:行者123 更新时间:2023-11-28 22:31:31 25 4
gpt4 key购买 nike

我的应用程序在 NSArray 中有很多字符串,用户通常可以单击与 ib 操作链接的按钮以转到下一个或上一个。当他们单击下一个或上一个时,TextView 中的文本将更改为数组中的下一个或上一个字符串。我希望用户能够在 TextView 中滑动以转到下一个或上一个。我已经了解了一些关于如何识别滑动的知识,但这需要一个全新的 class 继承 UITextView 而我的其他包含数组和 ib 操作的类继承 UIViewcontroller。我将发布我的代码的样子,我只想知道如何连接滑动类或在 Action 中识别滑动。感谢您的宝贵时间!

//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

最佳答案

您根本不必继承 UITextView,只需使用 UISwipeGestureRecognizer。在您的 View Controller 中,您将添加如下内容:

//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

您的 View Controller 现在将收到用户在 TextView 上滑动的通知。另外,this tutorial可能有助于澄清手势识别器。 编辑:您可以通过检查 ((UISwipeGestureRecognizer *)sender).direction 查询手势识别器(在 action 方法的 sender 参数中)的方向。

但是,如果您想使用 UITextView 路线,则必须添加一个使 View Controller 成为 TextView 的委托(delegate)并添加一个方法来表示滑动。在您的 TextView 子类的标题中,您将添加如下内容:

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

@end

当收到滑动时,自定义 TextView 将在委托(delegate)上调用这些方法,委托(delegate)(您的 View Controller )将执行您想要的任何操作。

希望这对您有所帮助!

关于ios - 如何在字符串数组中的 View Controller 内滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com