scrollView에서 터치 이벤트 받기
기본적으로 UIView에서는
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Ended");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Moved");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Cancelled");
}
를 이용하여 터치 이벤트를 처리할 수 있습니다.
그러나 UIView에 ScrollView를 올려놓게 되면 터치 이벤트가 ScrollView에서 처리하기 때문에
1. 우선 우리가 원하는 터치 이벤트와 뷰, 스크롤 뷰가 들어있는 클래스와 XIB파일을 만듭니다.
제가 만든 파일명은 ViewController입니다.
<ViewConroller.h>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>{
UIScrollView* scrollView;
}
@property(nonatomic, strong) IBOutlet UIScrollView* scrollView;
@end
#import "ViewController.h"
@end
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Ended");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Moved");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Cancelled");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *modelImageView = [[UIImageView alloc]init];
modelImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MODEL_List.jpg" ofType:nil]];
modelImageView.frame = CGRectMake(0, 0, 320, 6700);
scrollView.contentSize = CGSizeMake(320, 6700);
modelImageView.userInteractionEnabled = YES;
[scrollView addSubview:modelImageView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
2. 그리고 ViewController.xib파일에는 한 개의 스크롤뷰가 들어있는 뷰가 있습니다.
그리고 뷰 안의 스크롤뷰는 ViewController.h에서 IBOutlet으로 선언한 scrollView와 연결해 줍니다.
3. 이제 UIScrollView를 상속받은 클래스를 새로 생성합니다. 이 클래스는 xib파일을 필요로 하지 않으므로 xib파일 생성은 하지 않습니다.
제가 이 클래스에게 원하는 것은 터치 이벤트를 상위로 전달하는 것뿐이므로 불필요한 소스들은 넣지 않았습니다.
제가 만든 클래스의 이름은 ModelScrollViewController입니다.
#import <UIKit/UIKit.h>
@interface ModelScrollViewController : UIScrollView
#import "ModelScrollViewController.h"
@implementation ModelScrollViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return self;
}
- (void)didReceiveMemoryWarning
{
}
#pragma mark - View lifecycle
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.superview touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self.superview touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self.superview touchesCancelled:touches withEvent:event];
}
- (void)viewDidUnload
{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
상위 클래스인 ViewController로 이벤트를 넘겨주기 위해서 를[self.superview touchesBegan:touches withEvent:event]를 사용하였습니다.
4. 이 마지막으로 다시 ViewController.xib파일로 가서 scrollView의 custom class를 변경해 주도록 합니다.
'컴퓨터 > 아이폰' 카테고리의 다른 글
tableview 관련 (0) | 2012.11.26 |
---|---|
iOS 버전 바꾸기 (0) | 2012.11.19 |
맥os find 숨긴 파일 보이기 감추기 (0) | 2012.05.18 |
THEOS iOS hooking 툴 (0) | 2012.05.10 |
엔터프라이즈 계정 in house 배포 방식.. (0) | 2012.02.15 |