我想实现双击放大和缩小。所以在我的项目中我没有使用任何xib或故事板其纯粹的编程视图(硬编码)。 我的项目中没有scrollview,所以我添加了滚动视图,并将我的图像视图作为子视图添加到scoll-view中。
比我使用“UIGestureRecognizer”和“触摸开始”但当我按下双击我的应用程序崩溃。我不认识问题,因为我是IOS的新手请提出一些解决方案
这是我的代码
ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024,768)];
UIView *mixContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
mixContainerView.backgroundColor = [UIColor clearColor];
[mixContainerView addSubview:ScrollView];
backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
backgroundImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"app%03d_location%02d_background", location.application.id.intValue, location.id.intValue]];
[ScrollView addSubview:backgroundImageView];
- (void)tapTwice:(UIGestureRecognizer *)gestureRecognizer{
//on a double tap, call zoomToRect in UIScrollView
float newScale = [ScrollView zoomScale] * 1.5;
if (newScale > self.ScrollView.maximumZoomScale){
newScale = self.ScrollView.minimumZoomScale;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[ScrollView zoomToRect:zoomRect animated:YES];}
else{
newScale = self.ScrollView.maximumZoomScale;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[ScrollView zoomToRect:zoomRect animated:YES];}}
// Touch began for touch event to display and hide controll buttons
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(((UITouch *)[touches anyObject]).tapCount == 2)
{
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice)];
tapTwice.numberOfTapsRequired = 2;
//stops tapOnce from overriding tapTwice
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.view addGestureRecognizer:tapTwice];
else{
if(screenTimer)
{
[screenTimer invalidate];
screenTimer = nil;
}
mode1View.hidden=NO;
screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(turnOffScreen) userInfo:nil repeats:NO];}}
- (void)turnOffScreen{
NSLog(@"TURN OFF SCREEN");
if(screenTimer!=nil)
{
mode1View.hidden=YES;
}}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [ScrollView frame].size.height / scale;
zoomRect.size.width = [ScrollView frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
错误
unrecognized selector sent to instance 0x7b2f540
2013-03-13 18:46:09.960 VSL[9352:c07] *** Terminating app due to uncaught exception ' NSInvalidArgumentException', reason: '-[ViewController tapTwice]: unrecognized selector sent to instance 0x7b2f540'
*** First throw call stack:
(0x1f38012 0x1854e7e 0x1fc34bd 0x1f27bbc 0x1f2794e 0x75385a 0x75299b 0x7540df 0x756d2d 0x756cac 0x74ea28 0x4bb972 0x4bbe53 0x499d4a 0x48b698 0x1e93df9 0x1e93ad0 0x1eadbf5 0x1ead962 0x1edebb6 0x1eddf44 0x1edde1b 0x1e927e3 0x1e92668 0x488ffc 0x1fbd 0x1ee5 0x1)
libc++abi.dylib: terminate called throwing an exception
答案 0 :(得分:3)
您需要更改:
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice)];
要:
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)];
方法tapTwice:采用参数。