[英]Gesture recognizer and UIImageView
我發現此代碼可以在單擊圖像時更改圖像。
在.h中
@interface MyappViewController : UIViewController
{
NSDictionary *ddata;
UIImageView *firstImage;
}
@property(retain,nonatomic) IBOutlet UIImageView *firstImage;
在.m中
- (void)viewDidLoad
{
[super viewDidLoad];
firstImage.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(clickHandler:)];
pgr.delegate = self;
[firstImage addGestureRecognizer:pgr];
[pgr release];
// [self clickHandler:self];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* destroy the transition view, set the image first */
UIImageView *transitionImageView = (UIImageView *)context;
self.firstImage.image = transitionImageView.image;
[transitionImageView removeFromSuperview];
transitionImageView = nil;
}
- (void)clickHandler:(id)sender {
/* temporary view for the animation */
NSLog(@"Click Handled ");
UIImageView *transitionImageView = [[UIImageView alloc] initWithFrame:self.firstImage.frame];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[ddata objectForKey:@"pic"]]]];
transitionImageView.image = image;
transitionImageView.alpha = 0.0f;
[self.view addSubview:transitionImageView];
[UIView beginAnimations:@"UpdateImages" context:transitionImageView];
[UIView setAnimationDuration:2.0f];
transitionImageView.alpha = 1.0f;
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
當我單擊圖像時,什么也沒有發生,但是如果我調用[self clickHandler:self]; 在ViewDidLoad中,圖像會更改。 我的問題是單擊圖像時未處理該單擊。
代替UIPinchGestureRecognizer
您需要使用UITapGestureRecognizer
。 不要忘記設置所需的水龍頭數和手指數。 該文檔非常適合手勢識別器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.