【问题标题】:Scrolling UIScrollView on external screen attached to an iPad在连接到 iPad 的外部屏幕上滚动 UIScrollView
【发布时间】:2011-02-08 00:01:11
【问题描述】:

嘿。我已经实现了通过缩放制作程序化 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

正如我在下面所说的,我现在可以让滚动或缩放分别工作,但是如果滚动正常,缩放就会变得一团糟,因为在缩放时它认为它也在滚动。所以我想避免它在缩放时滚动,为此我想检测触摸次数,我一定做错了!

【问题讨论】:

  • 嗯,使用内容偏移 forums.macrumors.com/showthread.php?t=568315 并将这些坐标设置为另一个屏幕上的图像,我可能会有一些运气。
  • 我现在可以使用 scrollViewDidScroll 让它滚动并将坐标传递给外部屏幕。问题是,我不能同时进行缩放和滚动工作,因为这会导致缩放问题。我想到的一个解决方案是检测发生了多少次触摸,但开始的触摸甚至没有在 NSLog..grr 中显示任何内容

标签: ipad uiscrollview screen external television


【解决方案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 不起作用,堆栈溢出仍然损坏。 :)

【讨论】:

  • 您必须在每一行代码前放置4个空格。但是只需突出显示代码并按下{} 按钮会容易得多。
  • 谢谢,下次我会寻找 {} 按钮。
猜你喜欢
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
相关资源
最近更新 更多