0

and in this app I haw a sidescrolling/Paging scrollview with each "page" has an image and a textView. what I need to do is edit the textview on each page seperatly.

I also haw a TapGestureRecocgnizer that handles tap on the image I haw been trying to get this code to work fore a while so haw tried diferent things. handeling the show keyboard in the taphandler (works) but then I cant seem to get the "finnished editing" so I can save the text the user entered.

I haw tried to add a delegate for the textView both in the scrollviewcontroller that calls the "make subview" and in the subview that adds the img and textview to the view. this also almost works. tho only on the first or two first pages

- (void)viewDidAppear:(BOOL)animated {
[super viewDidLoad];    

UIView *subView = [[UIView alloc]initWithFrame:self.scrollViewOU.frame];

for(int i = 0; i < prgItm.slides.count; i++) {
    CGRect frame;

    frame.origin.x = self.scrollViewOU.frame.size.width * i ;
    frame.origin.y = 0.0;

    CGSize size = CGSizeMake(self.scrollViewOU.frame.size.width, self.scrollViewOU.frame.size.height);
    frame.size = size;
    self.scrollViewOU.pagingEnabled = YES;

    slide *sld = prgItm.slides[i];

    UIImage *img;

    NSString *imgName = sld.img;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *fullPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imgName];

    img = [UIImage imageWithContentsOfFile:fullPath];

    NSString *txt = [NSString stringWithFormat:@"%@", sld.notat];

    SlideView *slde = [[SlideView alloc] initWithImage:img note:txt frame:frame];

    [subView addSubview:slde];                

}    

[self.scrollViewOU addSubview:subView];
self.scrollViewOU.delaysContentTouches = YES;    

UITapGestureRecognizer *imgTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[imgTap setNumberOfTapsRequired:1];
[self.scrollViewOU addGestureRecognizer:imgTap];    

self.scrollViewOU.contentSize = CGSizeMake(self.scrollViewOU.frame.size.width * slides.count, self.scrollViewOU.frame.size.height );

CGPoint point = CGPointMake(scrollViewOU.frame.size.width * position, scrollViewOU.contentOffset.y);

[self.scrollViewOU setContentOffset:point animated:YES];

}

-(void)handleTap:(UITapGestureRecognizer *)Tap {


UIScrollView *view = Tap.view;
CGPoint point = [Tap locationInView:view];
int pageNr = round(view.contentOffset.x / view.frame.size.width);

UIView *slideView = view.subviews[0].subviews[pageNr];
UIView *touchedView = [slideView hitTest:point withEvent:nil];

NSLog(@"view.tag: %d", touchedView.tag);    
NSLog(@"point: %f %f" , point.x, point.y);
if(point.y >= slideView.subviews[1].frame.origin.y){
    UITextView *tmp = touchedView;
    //tmp.delegate = self;
    NSLog(@"touched text %@", tmp.text);

    //[tmp becomeFirstResponder];
    //[tmp reloadInputViews];
}else {
    [self performSegueWithIdentifier:@"ShowFullScreen" sender:self];
}

}

And the make Subview class

-(id)initWithImage:(UIImage *)slide note:(NSString *)text frame:(CGRect)frame
{
self = [super init];
if(self)
{
    CGRect rectView = CGRectMake(frame.origin.x,
                                 frame.origin.y,
                                 frame.size.width,
                                 frame.size.height);

    self.frame = rectView;
    CGRect imgRect = CGRectMake(0,
                                0,
                                frame.size.width,
                                frame.size.height * 0.75);

    UIImageView *subview = [[UIImageView alloc] initWithFrame:imgRect];

    subview.contentMode = UIViewContentModeScaleAspectFit;

    //[subview setAutoresizingMask:UIViewAutoresizingNone];
    subview.image = slide;

    subview.userInteractionEnabled = YES;
    subview.tag = 1;

    CGRect noteRect = CGRectMake(frame.size.width * 0.01,
                                 frame.size.height *0.76,
                                 frame.size.width * 0.98,
                                 frame.size.height *0.23);

    UITextView *noteView = [[UITextView alloc] initWithFrame:noteRect];

    [noteView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
    [noteView.layer setBorderWidth:2.0];

    noteView.layer.cornerRadius = 5;
    noteView.clipsToBounds = YES;

    noteView.userInteractionEnabled = YES;
    noteView.editable = YES;

    noteView.text = text;


    [noteView setKeyboardType:UIKeyboardTypeDefault];

    [noteView setDelegate:self];


    [self addSubview:subview];
    [self addSubview:noteView];


    self.userInteractionEnabled = YES;

        }
return self;
}
-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
//[textView becomeFirstResponder];
return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
return YES;
}

So to sum up. I need the scrollview to contain x pages with a Img and a textView. I need clicking the textview to start editing the textview. and I need a way to save the edited text when the user closes the keyboard. the page also need to react to tap on the img (this works)

JazeMan
  • 10
  • 7

2 Answers2

0

for X pages use the scrollView property pagingMode setting to true

for clicking the texview be sure that the UIUsersInteraction is enabled and you got delegate to textview and "implement the protocole" becomeFirstReponder

Please don't use tap to become responder of textView you goes in big troubles

You should remove tapGesture ASAP he probably got the click and forbidden you to get the call of texview responder

Rémi V.
  • 26
  • 4
  • I haw tried removing the tapgesture totaly and that does not change anything. I dont use it to "becomeFirstResponder" I only tried that once (actualy seemed to work, but cant get the editingdone to fire then) And paging works fine allready the images and textview show up. and I can swipe through the pages, I can tap the images only thing missing is editing the textview that only works on page 1(sometimes 2) – JazeMan Jan 11 '17 at 10:18
  • Last suggestion that may occur ( check the overlaps view ) a related post http://stackoverflow.com/questions/7361750/detecting-if-uiview-is-intersecting-other-uiviews – Rémi V. Jan 11 '17 at 10:22
  • wrote a function to check intersection now. and they do not intersect. will doublecheck my code to be shure. – JazeMan Jan 11 '17 at 10:41
  • 1
    You were not far off. after cursing this problem fore some time, I started to experiment. The problem was the parrent view did not haw the same width as the scrollview content. – JazeMan Jan 11 '17 at 13:17
0

So it turned out I needed to change the width of the parrent UIview to be the same as the total scrollview content size so (fram.width * pages)

this now works. it is a bit strange as I haw tried lots of things and the UIview that was the wrong size got added after I first encountered this problem.

JazeMan
  • 10
  • 7