在连接到iPad的外部屏幕上滚动UIScrollView

时间:2011-02-08 00:01:11

标签: ipad uiscrollview screen external television

嘿。我已经实现了一个带缩放的程序化UIScrollView,但现在我一直在尝试将可滚动/可缩放的图像插入到外部屏幕(如果已插入)。

@implementation MapVC

UIScrollView *mapScrollView;

UIImageView *mapImageView;

UIImageView *mapImageViewEx;

CGFloat lastScale = 0;

NSMutableArray *map_List;


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];

        map_List = [[NSMutableArray alloc] init];
        [map_List addObject:@"Pacific_Map.png"];
        [map_List addObject:@"Atlantic_Map.png"];


        CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);

        mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];

        mapScrollView.backgroundColor = [UIColor blackColor];

        [mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];

        mapScrollView.contentSize = CGSizeMake(2437, 1536);

        mapScrollView.bounces = NO;

        mapScrollView.bouncesZoom = NO;

        mapScrollView.minimumZoomScale = .5;

        mapScrollView.maximumZoomScale = 1.5;

        [mapScrollView setZoomScale:mapScrollView.minimumZoomScale];

        UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
        mapImageView = [[UIImageView alloc] initWithImage: mapImage];
        [mapImage release];

        if(exScreenEnabled==1){
            UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
            mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
            [mapImageEx release];

            UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
            [containerExViewP addSubview:mapImageViewEx];
        }else{
            [mapScrollView addSubview:mapImageView];
        }



        [self addSubview:mapScrollView];

        mapImageView.userInteractionEnabled = YES;



        UIImage *footerMapIMG = [UIImage imageNamed:@"footer_map_alternate.png"];
        UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
        CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
        footerMapView.frame = footerMapFrame;
        [self addSubview:footerMapView]; 
        footerMapView.image = footerMapIMG; 

        [footerMapView release];


        CGRect backBTNFrame = CGRectMake(20, 714, 140, 52); 
        UIButton *MAP_backButton = [[UIButton alloc] init];
        MAP_backButton.frame = backBTNFrame;
        UIImage *MAP_backButtonIMG = [UIImage imageNamed:@"button_back.png"];
        [MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
        MAP_backButton.backgroundColor = [UIColor clearColor];
        [self addSubview:MAP_backButton];

        [MAP_backButton release];


        [MAP_backButton addTarget:del.switchVC 
                           action:@selector(gotoMapAndListChooser)
                       forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if(exScreenEnabled==1){
        return mapImageViewEx;
    }else{
        return mapImageView;
    }
}

(对不起,我没有运气,因为在这个网站上看起来格式正确)

如果将视频线插入iPad,则iPad上没有图像,这就是我想要的。当您在iPad上执行手势时,外部屏幕上的图像会正确缩放,但我无法弄清楚如何使其滚动。提前谢谢。

编辑:我现在有了这个 -

@implementation MapVC

UIScrollView *mapScrollView;

UIImageView *mapImageView;
UIImageView *mapImageViewEx;

CGFloat lastScale = 0;

NSMutableArray *map_List;

int touchesNum = 0;

-(void)touchesBegan:(NSSet *)theTouches withEvent:(UIEvent *)event {
    NSSet *touches = [event allTouches];
    touchesNum=[touches count];
    NSLog(@"number of touches %i", touchesNum);
}


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {



        mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];

        map_List = [[NSMutableArray alloc] init];
        [map_List addObject:@"Pacific_Map.png"];
        [map_List addObject:@"Atlantic_Map.png"];


        CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);

        mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];

        mapScrollView.backgroundColor = [UIColor blackColor];

        [mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];

        mapScrollView.contentSize = CGSizeMake(2437, 1536);

        mapScrollView.bounces = NO;

        mapScrollView.bouncesZoom = NO;

        mapScrollView.minimumZoomScale = .5;

        mapScrollView.maximumZoomScale = 1.5;

        [mapScrollView setZoomScale:mapScrollView.minimumZoomScale];

        UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
        mapImageView = [[UIImageView alloc] initWithImage: mapImage];
        [mapImage release];

        if(exScreenEnabled==1){
            UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
            mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
            [mapImageEx release];

            UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
            [containerExViewP addSubview:mapImageViewEx];
        }else{
            [mapScrollView addSubview:mapImageView];
        }



        [self addSubview:mapScrollView];

        mapImageView.userInteractionEnabled = YES;



        UIImage *footerMapIMG = [UIImage imageNamed:@"footer_map_alternate.png"];
        UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
        CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
        footerMapView.frame = footerMapFrame;
        [self addSubview:footerMapView]; 
        footerMapView.image = footerMapIMG; 

        [footerMapView release];


        CGRect backBTNFrame = CGRectMake(20, 714, 140, 52); 
        UIButton *MAP_backButton = [[UIButton alloc] init];
        MAP_backButton.frame = backBTNFrame;
        UIImage *MAP_backButtonIMG = [UIImage imageNamed:@"button_back.png"];
        [MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
        MAP_backButton.backgroundColor = [UIColor clearColor];
        [self addSubview:MAP_backButton];

        [MAP_backButton release];


        [MAP_backButton addTarget:del.switchVC 
                           action:@selector(gotoMapAndListChooser)
                       forControlEvents:UIControlEventTouchUpInside];

        mapScrollView.multipleTouchEnabled = YES;
    }
    return self;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if(exScreenEnabled==1){
        return mapImageViewEx;
    }else{
        return mapImageView;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)inscrollView{
    if(touchesNum==0){
        CGPoint p = mapScrollView.contentOffset;
        mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageViewEx.frame.size.width, mapImageViewEx.frame.size.height); 
    }
}



- (void)dealloc {

    [mapScrollView release];
    [mapImageView release];

    [map_List release];


    [super dealloc];
}

@end

正如我在下面所说,我现在可以单独滚动或缩放,但如果滚动工作,则缩放全部搞砸了,因为缩放时它认为它也在滚动。所以我想避免它在缩放时滚动,为了做到这一点,我想检测触摸次数,我一定做错了!

1 个答案:

答案 0 :(得分:1)

使用iPad和外部屏幕上的图像。我可能会用矩形区域交换它,因为图像资源很重,无论是iPad还是外部屏幕。

#import "exGlobal.h"

#import "mapVC.h"

#import "switchVC.h"
#import "switchExVC.h"
#import "mainMenuAppDelegate.h"


#import <MobileCoreServices/MobileCoreServices.h>

@implementation MapVC

UIScrollView *mapScrollView;

UIImageView *mapImageView;
UIImageView *mapImageViewEx;

CGFloat lastScale = 0;

NSMutableArray *map_List;

static int toggleScroll = 1;


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];

        map_List = [[NSMutableArray alloc] init];
        [map_List addObject:@"Pacific_Map.png"];
        [map_List addObject:@"Atlantic_Map.png"];


        CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);

        mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];

        mapScrollView.backgroundColor = [UIColor blackColor];

        [mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];

        mapScrollView.contentSize = CGSizeMake(2437, 1536);

        mapScrollView.bounces = NO;

        mapScrollView.bouncesZoom = NO;

        mapScrollView.minimumZoomScale = .5;

        mapScrollView.maximumZoomScale = 1.5;

        [mapScrollView setZoomScale:mapScrollView.minimumZoomScale];

        UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
        mapImageView = [[UIImageView alloc] initWithImage: mapImage];
        [mapImage release];

        if(exScreenEnabled==1){
            UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
            mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
            [mapImageEx release];

            UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
            [containerExViewP addSubview:mapImageViewEx];
            [mapScrollView addSubview:mapImageView]; // see if this works ok on iPad. Map on TV AND iPad.
        }else{
            [mapScrollView addSubview:mapImageView];
        }

        [self addSubview:mapScrollView];

        mapImageView.userInteractionEnabled = YES;


        UIImage *footerMapIMG = [UIImage imageNamed:@"footer_map_alternate.png"];
        UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
        CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
        footerMapView.frame = footerMapFrame;
        [self addSubview:footerMapView]; 
        footerMapView.image = footerMapIMG; 

        [footerMapView release];


        CGRect backBTNFrame = CGRectMake(20, 714, 140, 52); 
        UIButton *MAP_backButton = [[UIButton alloc] init];
        MAP_backButton.frame = backBTNFrame;
        UIImage *MAP_backButtonIMG = [UIImage imageNamed:@"button_back.png"];
        [MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
        MAP_backButton.backgroundColor = [UIColor clearColor];
        [self addSubview:MAP_backButton];

        [MAP_backButton release];


        [MAP_backButton addTarget:del.switchVC 
                           action:@selector(gotoMapAndListChooser)
                       forControlEvents:UIControlEventTouchUpInside];

        mapScrollView.multipleTouchEnabled = YES;
    }
    return self;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return mapImageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if(exScreenEnabled==1){
        CGPoint p = mapScrollView.contentOffset;
        mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)inscrollView{   
    if(exScreenEnabled==1 && toggleScroll==1){
        CGPoint p = mapScrollView.contentOffset;
        mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);

    }
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)theScrollView withView:(UIView *)view{
    NSLog(@"BEGIN ZOOMING");
    toggleScroll=0;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)theScrollView withView:(UIView *)view atScale:(float)scale{
    NSLog(@"END ZOOMING");
    toggleScroll=1;
} 




- (void)dealloc {

    [mapScrollView release];
    [mapImageView release];

    [map_List release];

    [super dealloc];
}

@end

我按空格键(4)代码,NOPE不起作用,堆栈溢出仍然破坏。 :)