我在 UITableviewCell 中有一个 UIImageView.当它被点击时, UIImageView 应该动画以全屏显示.当图像在全屏时被点击时,它应该收缩回原来的位置.
I have an UIImageView in a UITableviewCell. When it is tapped, the UIImageView should animated to be displayed fullscreen. When the image is tapped when it is fullscreen it should shrink back to the original position.
如何做到这一点?
向视图控制器添加手势识别器.
Add a gesture recognizer to the view controller.
将手势识别器添加到您的头文件中
Add the gesture Recognizer to your header file
@interface viewController : UIViewController <UIGestureRecognizerDelegate>{
UITapGestureRecognizer *tap;
BOOL isFullScreen;
CGRect prevFrame;
}
在你的 viewDidLoad 中添加这个:
In your viewDidLoad add this:
isFullScreen = false;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
tap.delegate = self;
添加以下委托方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
BOOL shouldReceiveTouch = YES;
if (gestureRecognizer == tap) {
shouldReceiveTouch = (touch.view == yourImageView);
}
return shouldReceiveTouch;
}
现在您只需要实现您的 imgToFullScreen 方法.确保使用 isFullScreen Bool(如果为 false,则为全屏,如果为 true,则返回旧尺寸)
Now you just need to implement your imgToFullScreen method. Make sure you work with the isFullScreen Bool (fullscreen if it is false and back to old size if it's true)
imgToFullScreen 方法取决于您希望如何使图像变为全屏.一种方法是:(这是未经测试的,但应该可以工作)
The imgToFullScreen method depends on how you want to make the image become fullscreen. One way would be: (this is untested but should work)
-(void)imgToFullScreen{
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
//save previous frame
prevFrame = yourImageView.frame;
[yourImageView setFrame:[[UIScreen mainScreen] bounds]];
}completion:^(BOOL finished){
isFullScreen = true;
}];
return;
} else {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[yourImageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = false;
}];
return;
}
}
这篇关于如何通过点击动画 UIImageview 以显示全屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!