How to animate a UIImageview to display fullscreen by tapping on it?(如何通过点击动画 UIImageview 以显示全屏?)
问题描述
我在 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 以显示全屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过点击动画 UIImageview 以显示全屏?


基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01