關於iPad+iPhone上,圖片放大縮小時都能點擊此區域

比如我們有個中國地圖,在北京這個區域放了一個button,當這張地圖放大和縮小的時候,我們都能點擊這個button觸發北京裏面的事件。
方法有點笨。 總的來說就是計算放到後圖片的尺寸,然後算出放大倍數。把初始座標放大
設兩個變量
    float x;

    float y;


- (void)viewDidLoad {
    [super viewDidLoad];
    x = 1;
    y = 1;
  
    UIImage *image = [UIImage imageNamed:@"index_big.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, 1024, 768);
    imageView.userInteractionEnabled = YES;
  
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    scrollView.maximumZoomScale = 3.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    [self.view bringSubviewToFront:scrollView];
  
    [scrollView addSubview:imageView];
    [scrollView bringSubviewToFront:imageView];
}
  
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    uaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    uaButton.frame = CGRectMake(731*x, 235*y, 130, 60);
    [uaButton setTag:1];
    [uaButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:uaButton];
}
  
#pragma mark -
#pragma mark view zoom
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}
  
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
    NSLog(@"scrollViewDidEndZooming  width:%f, height:%f",imageView.frame.size.width, imageView.frame.size.height);
    x = imageView.frame.size.width/1024;
    y = imageView.frame.size.height/768;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章