問題描述
我在 UITableviewCell 中有一個 UIImageView.當它被點擊時, UIImageView 應(yīng)該動畫以全屏顯示.當圖像在全屏?xí)r被點擊時,它應(yīng)該收縮回原來的位置.
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;
}
現(xiàn)在您只需要實現(xiàn)您的 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 方法取決于您希望如何使圖像變?yōu)槿?一種方法是:(這是未經(jīng)測試的,但應(yīng)該可以工作)
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;
}
}
這篇關(guān)于如何通過點擊動畫 UIImageview 以顯示全屏?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!