1
所以綱領性的UIScrollView的錨固,我已經成功地創建與編程縮放方法一個UIScrollView,但我有點卡住了怎麼解決我與縮放有一個問題:邊界和與縮放
當我放大或縮小伸展/縮回位置的位置時,我在捏手勢的位置不會發生,它發生在一個角落。
放大或縮小後,會在邊界外留下額外的空間,並且我無法將圖像滾動超過圖像高度的一半寬度&。
除此之外,我很接近讓它工作100%。我嘗試過使用achorpoints,但它看起來像滾動視圖和圖像視圖不響應此。
這裏是重要的東西在代碼:
UIScrollView *mapScrollView;
UIImageView *mapImageView;
CGFloat lastScale = 0;
NSMutableArray *map_List;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:@"Pacific_Map_8bit.png"];
[map_List addObject:@"Atlantic_Map_8bit.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
mapScrollView.bounces = NO;
[mapImage release];
[mapScrollView addSubview:mapImageView];
[self addSubview:mapScrollView];
mapImageView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[mapImageView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
}
return self;
}
// the scale method thats triggered to zoom when pinching
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
NSLog(@"map scale %f", scale);
CGFloat mapWidth = mapImageView.frame.size.width;
CGFloat mapHeight = mapImageView.frame.size.height;
NSLog(@"map width %f", mapWidth);
if(scale>=1 & mapWidth<=4000 || scale<1 & mapWidth>=1234){
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
mapScrollView.contentSize = CGSizeMake(mapWidth, mapHeight);
}
謝謝!
即使我沒有使用接口生成器? – VagueExplanation 2011-01-28 23:39:02