Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to swipe within a view controller within in an an array of strings?
    primarykey
    data
    text
    <p>My app has so many strings in an <code>NSArray</code> and the user usually can click on a button which is linked with an ib action to go either next or previous. When they click next or previous the text within a <code>TextView</code> changes to the next or previous string within the array. I want the user to be able to swipe within the <code>TextView</code> to go either next or previous. I've learned a little bit about how to recognize swipes but that requires a whole new <code>class</code> which inherits <code>UITextView</code> while my other class that contains the array and ib actions inherit a <code>UIViewcontroller</code>. I'm going to post what my code looks like and I just want to know how to connect either the swipe class or have within the action to recognize the swipe. Thanks for your time!</p> <pre><code>//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&lt;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&gt;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 </code></pre> <p>SwipeableTextView.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #define kMinimumGestureLength 25 #define kMaximumVariance 5 typedef enum swipeDirection { kSwipeNone, kSwipeLeft, kSwipeRight } tSwipeDirection; @interface SwipeableTextView : UITextView { CGPoint gestureStartPoint; tSwipeDirection swipeDirection; } @end </code></pre> <p>SwipeableTextView.m</p> <pre><code>#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 &gt;= kMinimumGestureLength &amp;&amp; deltaY &lt;= kMaximumVariance &amp;&amp; swipeDirection == kSwipeNone) { if (gestureStartPoint.x &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload