在 ipad 上将图像切换为全屏

发布于 2024-10-31 04:28:13 字数 3209 浏览 3 评论 0原文

我在 iPad 上完成此任务遇到了很多麻烦:当在图像上双击该图像时,将该图像切换到全屏,并且当再次双击时返回到原始显示,使用捏合也是如此。我正在使用 UIGestureRecognizer 来尝试执行此操作。感谢您的帮助。

GesturesViewController.h
#import <UIKit/UIKit.h>

@interface GesturesViewController : UIViewController 
    <UIActionSheetDelegate>{
    IBOutlet UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

@end
GesturesViewController.m
    #import "GesturesViewController.h"
#import "GesturesAppDelegate.h"

@implementation GesturesViewController

@synthesize imageView;


CGRect originalFrame,fullScreenFrame;

BOOL isFullScreenMode;
- (void)viewDidLoad {
    // Loading test image
        imageView.image = [UIImage   imageNamed:@"image1.jpg"];
    //---tap gesture---
    isFullScreenMode = NO;

    originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);


    //changes
    fullScreenFrame = CGRectMake(0,0,768,1004);
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    //---pinch gesture---
    UIPinchGestureRecognizer *pinchGesture = 
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];   

    [super viewDidLoad];
}

//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
    // HOW TO ACCOMPLISH THIS PART
    if (isFullScreenMode)
        [imageView setFrame:originalFrame];
    else
        [imageView setFrame:fullScreenFrame];

    [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    isFullScreenMode = !isFullScreenMode;
    NSLog(@"Image View : %@",imageView);
}

//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender { 
    CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];   

    if (sender.state == UIGestureRecognizerStateEnded){
        // HOW TO ACCOMPLISH THIS ---
        if (factor > 1 && !isFullScreenMode) {
            //---pinching in---
            [imageView setFrame:fullScreenFrame];

        } else {
            //---pinching out---
            [imageView setFrame:originalFrame];

        } 
        isFullScreenMode = !isFullScreenMode;
        [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    }   
    NSLog(@"Image View : %@",imageView);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [images release];
    [imageView release];
    [super dealloc];
}

@end

谢谢。

I have a lot of trouble to accomplish this task on an ipad : when double tape on an image switch this image to full screen and when double taping again come back to the original display, same thing using pinching. I'm using UIGestureRecognizer to try to do this. Thanks for your help.

GesturesViewController.h
#import <UIKit/UIKit.h>

@interface GesturesViewController : UIViewController 
    <UIActionSheetDelegate>{
    IBOutlet UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

@end
GesturesViewController.m
    #import "GesturesViewController.h"
#import "GesturesAppDelegate.h"

@implementation GesturesViewController

@synthesize imageView;


CGRect originalFrame,fullScreenFrame;

BOOL isFullScreenMode;
- (void)viewDidLoad {
    // Loading test image
        imageView.image = [UIImage   imageNamed:@"image1.jpg"];
    //---tap gesture---
    isFullScreenMode = NO;

    originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);


    //changes
    fullScreenFrame = CGRectMake(0,0,768,1004);
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    //---pinch gesture---
    UIPinchGestureRecognizer *pinchGesture = 
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];   

    [super viewDidLoad];
}

//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
    // HOW TO ACCOMPLISH THIS PART
    if (isFullScreenMode)
        [imageView setFrame:originalFrame];
    else
        [imageView setFrame:fullScreenFrame];

    [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    isFullScreenMode = !isFullScreenMode;
    NSLog(@"Image View : %@",imageView);
}

//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender { 
    CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];   

    if (sender.state == UIGestureRecognizerStateEnded){
        // HOW TO ACCOMPLISH THIS ---
        if (factor > 1 && !isFullScreenMode) {
            //---pinching in---
            [imageView setFrame:fullScreenFrame];

        } else {
            //---pinching out---
            [imageView setFrame:originalFrame];

        } 
        isFullScreenMode = !isFullScreenMode;
        [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    }   
    NSLog(@"Image View : %@",imageView);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [images release];
    [imageView release];
    [super dealloc];
}

@end

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装爱人 2024-11-07 04:28:13
- (void)toggleZoom:(UITapGestureRecognizer *)gestureRecognizer
{
    if (proxyView)
    {
        CGRect frame =
            [proxyView.superview
                convertRect:self.view.frame
                fromView:self.view.window];
        self.view.frame = frame;

        CGRect proxyViewFrame = proxyView.frame;

        [proxyView.superview addSubview:self.view];
        [proxyView removeFromSuperview];
        [proxyView autorelease];
        proxyView = nil;
        self.view.frame = proxyViewFrame;

    }
    else
    {
        proxyView = [[UIView alloc] initWithFrame:self.view.frame];
        proxyView.hidden = YES;
        proxyView.autoresizingMask = self.view.autoresizingMask;
        [self.view.superview addSubview:proxyView];

        CGRect frame =
            [self.view.window
                convertRect:self.view.frame
                fromView:proxyView.superview];
        [self.view.window addSubview:self.view];
        self.view.frame = frame;
        self.view.frame = self.view.window.bounds;

    }


}

我只选择了代码的必要部分......它来自 ZoomingViewController......
如果您看到它与我们之前讨论的相同......但几乎没有改进......

- (void)toggleZoom:(UITapGestureRecognizer *)gestureRecognizer
{
    if (proxyView)
    {
        CGRect frame =
            [proxyView.superview
                convertRect:self.view.frame
                fromView:self.view.window];
        self.view.frame = frame;

        CGRect proxyViewFrame = proxyView.frame;

        [proxyView.superview addSubview:self.view];
        [proxyView removeFromSuperview];
        [proxyView autorelease];
        proxyView = nil;
        self.view.frame = proxyViewFrame;

    }
    else
    {
        proxyView = [[UIView alloc] initWithFrame:self.view.frame];
        proxyView.hidden = YES;
        proxyView.autoresizingMask = self.view.autoresizingMask;
        [self.view.superview addSubview:proxyView];

        CGRect frame =
            [self.view.window
                convertRect:self.view.frame
                fromView:proxyView.superview];
        [self.view.window addSubview:self.view];
        self.view.frame = frame;
        self.view.frame = self.view.window.bounds;

    }


}

I have selected only necessary portion of the code...... its from ZoomingViewController....
If you see its same as we have discussed earlier..... but with few improvements.......

酸甜透明夹心 2024-11-07 04:28:13

为此,您必须首先将原始帧大小存储在全局某个位置,以便稍后可以重新评估它。

you need create two global frames

    CGRect originalFrame, fullScreenFrame;

    //in viewDidLoad initialize these frames... originalFrame with imageView frame and 
fullScreenFrame with the iPad window coordinates........ but remeber this can distort the
 aspect ratio so just calculate the aspect ratio of original image by using its height and
 width and accordingly create the full screen frame for the image....... 

    and just assign these frames in your gesture action.

谢谢,

For doing this you have to first store your original frame size somewhere globally so that you can reassing it later on.

you need create two global frames

    CGRect originalFrame, fullScreenFrame;

    //in viewDidLoad initialize these frames... originalFrame with imageView frame and 
fullScreenFrame with the iPad window coordinates........ but remeber this can distort the
 aspect ratio so just calculate the aspect ratio of original image by using its height and
 width and accordingly create the full screen frame for the image....... 

    and just assign these frames in your gesture action.

Thanks,

怪我鬧 2024-11-07 04:28:13

in

viewDidLoad
originalFrame = imageView.frame;

originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);

appDelegate <----- 获取 appdelegate 对象的实例,以便我们可以检索窗口对象.......

UIWindow *tempWindow = [appDelegate window];
fullScreenFrame = CGRectMake(tempWindow .frame.origin.x,tempWindow .frame.origin.y,tempWindow .frame.size.width,tempWindow.frame.size.height);

//**in 事件只是设置框架imageView--为了了解当前状态--无论是全屏还是原始帧,我们都需要有一个标志……它应该是全局的……
因此,声明一个全局标志 ....BOOL isFullScreenMode 并将其初始化为 NO

viewDidLoad
isFullScreenMode = NO;

手势操作中只需检查此标志并编写以下内容...

if (isFullScreenMode)
        [imageView setFrame:originalFrame];
else
       [imageView setFrame:fullScreenFrame];

isFullScreenMode = !isFullScreenMode;

in

viewDidLoad
originalFrame = imageView.frame;

or

originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);

appDelegate <----- get the instance of your appdelegate object so that we can retrieve the window object.......

UIWindow *tempWindow = [appDelegate window];
fullScreenFrame = CGRectMake(tempWindow .frame.origin.x,tempWindow .frame.origin.y,tempWindow .frame.size.width,tempWindow.frame.size.height);

//**in event just set frame of the imageView-- for knowing the current state-- whether its fullScreen or original frame we need to have a flag........it should be global...
so declare a global flag ....BOOL isFullScreenMode and initialize it as NO in

viewDidLoad
isFullScreenMode = NO;

in gesture actions just check this flag and write following...

if (isFullScreenMode)
        [imageView setFrame:originalFrame];
else
       [imageView setFrame:fullScreenFrame];

isFullScreenMode = !isFullScreenMode;
⊕婉儿 2024-11-07 04:28:13
@implementation ImageFullScreen
@synthesize myImage;

#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"

@implementation GesturesViewController

@synthesize imageView;

CGRect originalFrame,fullScreenFrame;

BOOL isFullScreenMode;
- (void)viewDidLoad {
    // Loading test image
    imageView.image = [UIImage   imageNamed:@"image1.jpg"];
    //---tap gesture---
    isFullScreenMode = NO;

    originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);


    //changes
    fullScreenFrame = CGRectMake(0,0,768,1004);
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    //---pinch gesture---
    UIPinchGestureRecognizer *pinchGesture = 
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];   

    [super viewDidLoad];
}

//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
    // HOW TO ACCOMPLISH THIS PART
    if (isFullScreenMode)
        [imageView setFrame:originalFrame];
    else
        [imageView setFrame:fullScreenFrame];

    [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    isFullScreenMode = !isFullScreenMode;
    NSLog(@"Image View : %@",imageView);
}

//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender { 
    CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];   

    if (sender.state == UIGestureRecognizerStateEnded){
        // HOW TO ACCOMPLISH THIS ---
        if (factor > 1 && !isFullScreenMode) {
            //---pinching in---
            [imageView setFrame:fullScreenFrame];

        } else {
            //---pinching out---
            [imageView setFrame:originalFrame];

        } 
        isFullScreenMode = !isFullScreenMode;
        [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    }   
    NSLog(@"Image View : %@",imageView);
}
@implementation ImageFullScreen
@synthesize myImage;

#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"

@implementation GesturesViewController

@synthesize imageView;

CGRect originalFrame,fullScreenFrame;

BOOL isFullScreenMode;
- (void)viewDidLoad {
    // Loading test image
    imageView.image = [UIImage   imageNamed:@"image1.jpg"];
    //---tap gesture---
    isFullScreenMode = NO;

    originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);


    //changes
    fullScreenFrame = CGRectMake(0,0,768,1004);
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    //---pinch gesture---
    UIPinchGestureRecognizer *pinchGesture = 
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];   

    [super viewDidLoad];
}

//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
    // HOW TO ACCOMPLISH THIS PART
    if (isFullScreenMode)
        [imageView setFrame:originalFrame];
    else
        [imageView setFrame:fullScreenFrame];

    [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    isFullScreenMode = !isFullScreenMode;
    NSLog(@"Image View : %@",imageView);
}

//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender { 
    CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];   

    if (sender.state == UIGestureRecognizerStateEnded){
        // HOW TO ACCOMPLISH THIS ---
        if (factor > 1 && !isFullScreenMode) {
            //---pinching in---
            [imageView setFrame:fullScreenFrame];

        } else {
            //---pinching out---
            [imageView setFrame:originalFrame];

        } 
        isFullScreenMode = !isFullScreenMode;
        [imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    }   
    NSLog(@"Image View : %@",imageView);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文