IOS學習之UIScrollView touch觸摸事件是本文要介紹的內容,UIScrollView本身無法處理touch事件。要想實現,必須對UIScrollView上的subView做touch處理原理十分簡單,好比要響應scrollView上的UIImageView,那麼請建立一個UIImageVIew的子類,由這個自訂的UIImageView來處理touch事件。
標頭檔聲明如下,供參考:
- #import <Foundation/Foundation.h>
-
- @protocol ImageTouchDelegate
- -(void)imageTouch:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)imageView;
- @end
-
- @interface ImageTouchView : UIImageView
- {
- id<ImageTouchDelegate> delegate;
- BOOL delegatrue;
- }
- @property(nonatomic,assign)id<ImageTouchDelegate> delegate;
- @end
這個是標頭檔,源檔案可以是這個這樣子
- @implementation ImageTouchView
- @synthesize delegate;
- -(id)initWithFrame:(CGRect)frame
- {
- if (self == [super initWithFrame:frame])
- {
- [self setUserInteractionEnabled:YES];
- delegatrue=YES;
- }
- return self;
- }
- - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
- {
- return YES;
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (delegatrue)
- {
- [delegate imageTouch:touches withEvent:event whichView:self];
- }
小結:IOS學習之UIScrollView touch觸摸事件的內容介紹完了,希望本文對你有所協助!