並且在這個應用程序中我擁有一個sidescrolling/Paging滾動視圖,每個「頁面」都有一個圖像和一個textView。我需要做的是分別在每個頁面上編輯textview。Objective C Textview在滾動視圖中不會觸發textViewShouldBeginEditing
我也山楂,處理圖像 我山楂一直試圖讓這個代碼前一段時間才能使山楂試圖diferent事情上抽頭TapGestureRecocgnizer。在製作Taphandler(作品)時,我看到了顯示鍵盤,但後來似乎無法獲得「Finnished編輯」,因此我可以保存用戶輸入的文字。
我試圖在調用「make子視圖」的子視圖中添加img和textview到視圖的scrollviewcontroller中爲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;
}
所以總結起來。 我需要滾動視圖包含X頁與Img和textView。 我需要點擊textview才能開始編輯textview。當用戶關閉鍵盤時,我需要一種方法來保存編輯後的文本。該頁面還需要能夠應對輕觸IMG(這部作品)
我曾嘗試刪除tapgesture totaly,並沒有改變任何東西。我不使用它來「成爲FirstResponder」我只嘗試過一次(actualy似乎工作,但不能得到編輯後開火然後) 和分頁工作正常圖像和文本顯示。並且我可以在頁面上滑動,我可以點擊圖片只有缺少的東西是編輯僅適用於頁面1(有時是2)的textview – JazeMan
最後一個可能發生的建議(檢查重疊視圖)一個相關的帖子http:// stackoverflow .com/questions/7361750 /正在檢測if-uiview-is-intersecting-other-uiviews –
現在寫了一個函數來檢查交集。和它們不相交。將雙重檢查我的代碼是否舒適。 – JazeMan