自定义UIScrollView,添加触摸事件

好久没更新了,之前一直在忙一个项目,现在有空了赶紧补上。

用了代理为其添加image和label,本来想在代理里面给image添加触摸事件,但是考虑到要触发的方法在ViewController类里面,所以就把触摸响应的方法注销了,各位在看代码的时候请注意

下面是效果图

 

先写了一个代理的类UITouchScrollView.h

#import <UIKit/UIKit.h>

@protocol UIScrollViewTouchesDelegate <NSObject>

-( void)scrollViewTouchesEnded:(NSSet *)touches withEvent:(UIEvent *) event whichView:( id)scrollView; 

@end

@interface UITouchScrollView : UIScrollView
{
    UIImageView *imageview;
    UILabel *label;
}

@property (assign,nonatomic)id<UIScrollViewTouchesDelegate>touchesDelegate;
@property (retain,nonatomic)UIImage *image;
@property (retain,nonatomic)UILabel *label;

//- (id)initWithImage:(UIImage *)img;
@end

UITouchScrollView.m

#import "UITouchScrollView.h"

@implementation UITouchScrollView
@synthesize touchesDelegate = _touchesDelegate;
@synthesize image;
@synthesize label;
//- ( void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *)  event {
//    
//    if (!self.dragging) {
//        // run at ios5 ,no effect;
//        [self.nextResponder touchesEnded: touches withEvent: event]; 
//        if (_touchesDelegate!=nil) {
//            
//            [_touchesDelegate scrollViewTouchesEnded:touches withEvent: event whichView:self];
//        }
//        NSLog( @" UITouchScrollView nextResponder touchesEnded ");
//    }        
//    [super touchesEnded: touches withEvent:  event];
//    
//} 

- (id)init {
    self = [super init];
    if (self) {
        imageview =  [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80.f, 80.f)]; 
        [self addSubview:imageview];

        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 85, 80, 15)];
        NSLog(@"%@",NSStringFromCGRect(label.frame));
        [self addSubview:label];
    }
    return self;
}

- (void)setImage:(UIImage *)img
{
    imageview.image = img;
}

- (void)setLabel:(UILabel *)lab
{
    label = lab;
}

- (void)dealloc {
    [imageview release];
    [label release];
    [super dealloc];
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "UITouchScrollView.h"

@interface ViewController : UIViewController<UIScrollViewTouchesDelegate,UIScrollViewDelegate>
{
    
}
- (void)showAlert:(UITapGestureRecognizer*)gesture;
@property (retain,nonatomic)UITouchScrollView *myScrollView;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize myScrollView;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.delegate = self;
    scroll.scrollEnabled = YES;
    [scroll setContentSize:CGSizeMake(800, 200)];//UIScrollView的尺寸必须小于ContentSize才会滚动
    for (int i = 0; i < 8; i++) {
        NSString *imgPath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",i+1] ofType:@"jpg"];//图片的名字是@"1.jpg",@"2.jpg"...的格式
        UITouchScrollView *myscroll = [[UITouchScrollView alloc] init];
        myscroll.image = [UIImage imageWithContentsOfFile:imgPath];
        myscroll.frame = CGRectMake(100 * i, 100, 100, 120);
        myscroll.tag = 1000 + i;
        [myscroll.label setText:[NSString stringWithFormat:@"tag=%d",myscroll.tag]];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showAlert:)];
        tapGesture.view.tag = myscroll.tag;
        [myscroll addGestureRecognizer:tapGesture];
        [scroll addSubview:myscroll];
        self.myScrollView = myscroll;
        [myscroll release];
        [tapGesture release];
    }
    [self.view addSubview:scroll];
}

- (void)showAlert:(UITapGestureRecognizer*)gesture
{
    NSString *mes = [NSString stringWithFormat:@"%d",gesture.view.tag];

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

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

- (void)dealloc {
    
    [super dealloc];
}

@end

 

posted @ 2013-01-05 12:37  小白猪jianjian  阅读(935)  评论(0编辑  收藏  举报