【问题标题】:objective c Textview in scrollview not fireing textViewShouldBeginEditing滚动视图中的目标c Uitextview不触发textViewShouldBeginEditing
【发布时间】:2017-05-26 00:44:50
【问题描述】:

在这个应用程序中,我拥有一个横向滚动/分页滚动视图,每个“页面”都有一个图像和一个 textView。我需要做的是分别编辑每个页面上的文本视图。

我还有一个 TapGestureRecocgnizer 可以处理点击图像 我一直试图让这段代码工作一段时间,所以尝试了不同的东西。在 Taphandler 中处理显示键盘(有效),但我似乎无法获得“完成的编辑”,因此我可以保存用户输入的文本。

我尝试在调用“make subview”的滚动视图控制器和将 img 和 textview 添加到视图的子视图中为 textView 添加一个委托。 这也几乎有效。仅在第一页或两页上

- (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];
}

}

以及制作子视图类

-(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;
}

总结一下。 我需要滚动视图包含带有 Img 和 textView 的 x 页。 我需要单击 textview 开始编辑 textview。当用户关闭键盘时,我需要一种方法来保存编辑后的文本。页面还需要对点击 img 做出反应(这可行)

【问题讨论】:

    标签: ios objective-c uiscrollview uitextview


    【解决方案1】:

    结果我需要将父 UIview 的宽度更改为与总滚动视图内容大小相同(fram.width * pages)

    现在可以了。这有点奇怪,因为我尝试了很多东西,并且在我第一次遇到这个问题后添加了错误大小的 UIview。

    【讨论】:

      【解决方案2】:

      对于 X 页面,使用 scrollView 属性 pagingMode 设置为 true

      对于单击 texview,请确保 UIUsersInteraction 已启用,并且您已委托 textview 并“实现协议”成为FirstReponder

      请不要使用点击成为你进入大的 textView 的响应者 烦恼

      你应该尽快删除 tapGesture 他可能得到了点击并禁止你得到 texview 响应者的调用

      【讨论】:

      • 我尝试完全删除 Tapgesture,但这并没有改变任何东西。我不使用它来“成为FirstResponder”我只尝试过一次(实际上似乎有效,但无法让editdone触发)并且分页工作正常,图像和textview显示出来。我可以浏览页面,我可以点击图像唯一缺少的是编辑仅适用于第 1 页(有时 2)的文本视图
      • 可能出现的最后建议(检查重叠视图)相关帖子stackoverflow.com/questions/7361750/…
      • 现在写了一个检查交叉点的函数。他们不相交。将仔细检查我的代码是否是舒尔。
      • 离你不远了。在诅咒这个问题一段时间后,我开始进行实验。问题是父视图与滚动视图内容的宽度不同。
      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多