Tap Gesture does not always work - ios

I have a NSObject that instantiate a view with a UITapGestureRecognizer added. Something goes wrong because the tap works only sometimes...
#import "Monument.h"
#implementation Monument
-(id)initWithFrame:(CGRect)frame itiName:(NSString *)itiName itiPay:(BOOL)itiPay available:(BOOL)available imageName:(NSString *)imageName viewController:(UIViewController *)controller
{
self = [super init];
if (self)
{
self.view = [[UIView alloc] initWithFrame:frame];
self.view.userInteractionEnabled = YES;
self.itiName = itiName;
self.itiPay = itiPay;
self.itiImage = imageName;
self.controller = controller;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:itiName])
{
self.buy = NO;
}
else
{
self.buy = YES;
}
self.x = frame.origin.x;
self.y = frame.origin.y;
self.available = available;
}
return self;
}
-(UIView *)drawView
{
UIImage *background = [UIImage imageNamed:self.itiImage];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:background];
backgroundView.frame = self.view.frame;
backgroundView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:backgroundView];
// Inzializzo la Label del titolo
UILabel *titolo = [[UILabel alloc] initWithFrame:CGRectMake(self.x+8.0f, self.y+2.0f, 180.0f, 20.0f)];
titolo.text = self.itiName;
titolo.textColor = [UIColor whiteColor];
[self.view addSubview:titolo];
UIImage *imageFreeOrPayOrBought;
if (! self.buy)
{
if (self.itiPay)
{
imageFreeOrPayOrBought = [UIImage imageNamed:#"pay-ipad"];
}
else
{
imageFreeOrPayOrBought = [UIImage imageNamed:#"free-ipad"];
}
}
else
{
// giĆ  comprato
imageFreeOrPayOrBought = [UIImage imageNamed:#"free-ipad"];
}
UIImageView *payOrFree = [[UIImageView alloc] initWithImage:imageFreeOrPayOrBought];
CGRect frameImage = CGRectMake(self.x, self.y+117.0f, 214.5f, 20.8f);
payOrFree.frame = frameImage;
payOrFree.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:payOrFree];
// Controllo se disponibile o meno
if (! self.available)
{
self.view.layer.opacity = .3f;
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[self.view addGestureRecognizer:tap];
return self.view;
}
-(void)tapRecognized: (UITapGestureRecognizer *)tap
{
if (! self.available)
{
// [self openUnavailableService];
NSLog(#"Service unavailable");
}
else
{
if (self.buy)
{
//[self openMonumentHomePage];
NSLog(#"Service bought");
}
else
{
if (self.itiPay)
{
//[self openPurchaseInApp];
NSLog(#"Service to buy");
}
else
{
//[self downloadDataForFree];
NSLog(#"Service to free download");
}
}
}
}
And that's how I use the class
self.arrayOfMonuments = [[NSMutableArray alloc] init];
self.scroll.contentSize = CGSizeMake(568.0f, 640.0f);
Monument *foroRomano = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 217.1f, 117.0f) itiName:#"FORO ROMANO" itiPay:NO available:YES imageName:#"fori-romano-ipad" viewController:self];
[self.arrayOfMonuments addObject:foroRomano];
UIView *foroRomanoView = [foroRomano drawView];
[self.scroll addSubview:foroRomanoView];
Monument *colosseo = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 0.0f, 217.1f, 117.0f) itiName:#"COLOSSEO" itiPay:NO available:YES imageName:#"colosseo-ipad" viewController:self];
[self.arrayOfMonuments addObject:colosseo];
[self.scroll addSubview:[colosseo drawView]];
Monument *appiaAntica = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 80.0f, 217.1f, 117.0f) itiName:#"APPIA ANTICA" itiPay:YES available:NO imageName:#"appia-antica-ipad" viewController:self];
[self.arrayOfMonuments addObject:appiaAntica];
UIView *appiaAnticaView = [appiaAntica drawView];
[self.scroll addSubview:appiaAnticaView];
Monument *esquilino = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 80.0f, 217.1f, 117.0f) itiName:#"ESQUILINO" itiPay:YES available:NO imageName:#"esquilino-ipad" viewController:self];
[self.arrayOfMonuments addObject:esquilino];
[self.scroll addSubview:[esquilino drawView]];
Monument *palatino = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 160.0f, 217.1f, 117.0f) itiName:#"PALATINO" itiPay:YES available:NO imageName:#"palatino-ipad" viewController:self];
[self.arrayOfMonuments addObject:palatino];
[self.scroll addSubview:[palatino drawView]];
Monument *laterano = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 160.0f, 217.1f, 117.0f) itiName:#"LATERANO" itiPay:YES available:NO imageName:#"laterano-ipad" viewController:self];
[self.arrayOfMonuments addObject:laterano];
[self.scroll addSubview:[laterano drawView]];
Monument *foriImperiali = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 240.0f, 217.1f, 117.0f) itiName:#"FORI IMPERIALI" itiPay:YES available:NO imageName:#"fori-imperiali-ipad" viewController:self];
[self.arrayOfMonuments addObject:foriImperiali];
[self.scroll addSubview:[foriImperiali drawView]];
How can it happens? Thanks a lot
EDIT: I also tried to replacee the UITapGesture with a UIButton, but problem still presents
UIButton *monumentBtn = [[UIButton alloc] initWithFrame:self.view.frame];
monumentBtn.backgroundColor = [UIColor clearColor];
[self.view addSubview:monumentBtn];
[monumentBtn addTarget:self action:#selector(tapRecognized:) forControlEvents:UIControlEventTouchUpInside];
EDIT: I noticed that the x/y coordinates that I pass to init method are wrong because the first frame's height is 117.0f and the view that is positioned under this has 80.0f y origin. That's strange, and that's why the Tap does not always work. How can I solve this issue?
RE-EDIT: I solved with a mapping of the screen...unfortunately I didn't find other solutions.

Related

animation not works in the non-initial UIViewController

I have a UIView called loadingView shows some UIActivityIndicatorView-like animation. There are two UIViewControllers that all have such UIView.
The initial UIViewController's code:
- (void)viewDidLoad {
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
[self.view addSubview:self.loadingView];
}
Also it has a button to present the second UIViewController:
- (void)buttonPressed {
SecondViewController *sVC = [[SecondViewController alloc] init];
[self presentViewController:sVC animated:YES completion:nil];
}
The second ViewController's viewDidLoad method code:
- (void)viewDidLoad {
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
[self.view addSubview:self.loadingView];
}
As you can see, the two UIViewControllers has the same viewDidLoad method code.
The loadingView works well in the initial UIViewController.
But in the second UIViewController, the loadingView only displays the black frame( I set the background colour to black).
Here is my loadingView implement code:
#define NUMBER_OF_DOT 15
#define DURATION 1.5
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.replicatorLayer = [[CAReplicatorLayer alloc] init];
self.replicatorLayer.frame = frame;
self.replicatorLayer.cornerRadius = 10.0;
self.replicatorLayer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.86].CGColor;
self.replicatorLayer.position = self.center;
self.replicatorLayer.instanceDelay = DURATION/NUMBER_OF_DOT;
[self.layer addSublayer:self.replicatorLayer];
float size = frame.size.width*14/200;
self.dot = [[CALayer alloc] init];
self.dot.bounds = CGRectMake(0, 0, size, size);
self.dot.position = CGPointMake(frame.size.width/2, frame.size.height/5);
self.dot.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
self.dot.borderColor = [UIColor whiteColor].CGColor;
self.dot.borderWidth = 1.0;
self.dot.cornerRadius = 1.5;
self.dot.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);
[self.replicatorLayer addSublayer:self.dot];
self.replicatorLayer.instanceCount = NUMBER_OF_DOT;
float angle = 2*M_PI/NUMBER_OF_DOT;
self.replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0.0, 0.0, 0.1);
self.shrink = [[CABasicAnimation alloc] init];
self.shrink.keyPath = #"transform.scale";
self.shrink.fromValue = [NSNumber numberWithFloat:1.0];
self.shrink.toValue = [NSNumber numberWithFloat:0.1];
self.shrink.duration = DURATION;
self.shrink.repeatCount = INFINITY;
[self.dot addAnimation:self.shrink forKey:nil];
}
return self;
}
-(void)viewDidAppear:(BOOL)animated
{
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
[self.view addSubview:self.loadingView];
}
Put this code in viewDidAppear of SecondViewController so it works in present mode as well.

How to sync between [plot reloadData] and annotation in core plot?

My requirement is, on dragging the vertical line - it should slide along with the < > arrow buttons.The view at the right end becomes of green transparent.
My issue is,sometimes when I drag the vertical line ,the line,buttons are mis aligned as shown in picture 2.Could anyone help to resolve my issue.
Implementation of plot line drag event.enter code here
- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)point
{
CPTScatterPlot *freqRolPlot = (CPTScatterPlot *)[self.curveEditingGraphView.hostedGraph plotAtIndex:2];
CGPoint pointInPlotArea = [self.curveEditingGraphView.hostedGraph convertPoint:point toLayer:self.curveEditingGraphView.hostedGraph.plotAreaFrame];
NSInteger indexVal = [thePlot indexOfVisiblePointClosestToPlotAreaPoint:pointInPlotArea];
if(pointInPlotArea.x <=24)
{
indexVal = 0;
pointInPlotArea.x = 14.0f;
}
NSDictionary* num = [self.editableGraphData objectAtIndex:indexVal];
if(pointInPlotArea.x <=24)
{
freqRollCoordinate = 20;
}
else{
freqRollCoordinate = [[num objectForKey:#"x"] integerValue];
}
[self showDisabledView:pointInPlotArea.x];
[self showFreqRolAnnotation:freqRollCoordinate:pointInPlotArea.x];
[freqRolPlot reloadData];
return YES;
}
-(void)showDisabledView:(float)selectedAreaXPos
{
CPTGraph *graph = self.curveEditingGraphView.hostedGraph;
if (self.maskAnnotation ) {
[graph.plotAreaFrame.plotArea removeAnnotation:self.maskAnnotation];
self.maskAnnotation = nil;
[nxtBtn removeFromSuperview];
[prevBtn removeFromSuperview];
}
prevBtn = [UIButton buttonWithType: UIButtonTypeCustom];
prevBtn.frame = CGRectMake(selectedAreaXPos-7,18,30,30);
UIImage *btnImage = [UIImage imageNamed:#"PrevbtnImg"];
[prevBtn setImage:btnImage forState:UIControlStateNormal];
nxtBtn = [UIButton buttonWithType: UIButtonTypeCustom];
nxtBtn.frame = CGRectMake(3,15,30,30);
UIImage *nxtImage = [UIImage imageNamed:#"NextbtnImg"];
[nxtBtn setImage:nxtImage forState:UIControlStateNormal];
CGRect imageRect = CGRectMake(selectedAreaXPos,0,self.curveEditingGraphView.hostedGraph.bounds.size.width-selectedAreaXPos,self.curveEditingGraphView.hostedGraph.bounds.size.height);
CPTBorderedLayer * imageLayer = [[CPTBorderedLayer alloc] initWithFrame:imageRect];
CPTColor *transparentGreen = [[CPTColor greenColor] colorWithAlphaComponent:CPTFloat(0.4)];
CPTFill* areaFill = [[CPTFill alloc] initWithColor:transparentGreen];
imageLayer.fill = areaFill;
[imageLayer addSublayer:nxtBtn.layer];
self.maskAnnotation =[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:nil];
self.maskAnnotation.contentLayer = imageLayer;
[graph addSublayer:prevBtn.layer];
[self.curveEditingGraphView.hostedGraph.plotAreaFrame.plotArea addAnnotation:self.maskAnnotation];
}

UIImageView not responding to gestures on certain edges

I am making a custom control which consists of UIImageView and Label. The control behaves like a checkbox.
Here is the code:
-(instancetype) initWithTitle:(NSString *)title normalCheckBoxImage:(NSString *)normalCheckBoxImage selectedCheckBoxImage:(NSString *)selectedCheckBoxImage
{
self = [super init];
self.userInteractionEnabled = YES;
self.normalCheckBoxImageName = normalCheckBoxImage;
self.selectedCheckBoxImageName = selectedCheckBoxImage;
self.title = title;
[self setupSubViews];
[self registerGestureRecognizers];
return self;
}
-(void) registerGestureRecognizers
{
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkBoxTapped:)];
singleTapGestureRecognizer.delegate = self;
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[self.checkBoxImageView addGestureRecognizer:singleTapGestureRecognizer];
}
-(void) checkBoxTapped:(UITapGestureRecognizer *) recognizer
{
self.isChecked = !self.isChecked;
if(self.isChecked)
{
[self.checkBoxImageView setImage:[UIImage imageNamed:self.selectedCheckBoxImageName]];
}
else
{
[self.checkBoxImageView setImage:[UIImage imageNamed:self.normalCheckBoxImageName]];
}
}
-(void) setupSubViews
{
self.checkBoxImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.normalCheckBoxImageName]];
self.checkBoxImageView.frame = CGRectMake(0, 0, 50, 50);
NSLog(#"width = %f; height = %f",self.checkBoxImageView.frame.size.width,self.checkBoxImageView.frame.size.height);
[self.checkBoxImageView setUserInteractionEnabled:YES];
// The init with frame should be replaced by the NSConstraints
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.checkBoxImageView.frame.size.width + 5, 10, 100, 20)];
self.titleLabel.text = self.title;
[self addSubview:self.checkBoxImageView];
[self addSubview:self.titleLabel];
}
I have attached gesture recognizer to the UIImageView. But for some reason it only fires when I am close to the left edge of the UIImageView. I am not sure what I am doing wrong. If I touch in the middle of the uIImageView then it does not do anything.
Here is the image:
The problem was the frame for the super view was not correct. Instead of width and height of 50 X 50 it was 50 X 20.
AZCheckBox *checkbox = [[AZCheckBox alloc] initWithTitle:#"I agree" normalCheckBoxImage:#"NormalCheckBoxImage" selectedCheckBoxImage:#"SelectedCheckBoxImage"];
checkbox.frame = CGRectMake(100, 100, 200, 50);
checkbox.delegate = self;
[self.view addSubview:checkbox];

Remove and release an type object from a view

I have a class call Post and its a view....i add it some times in my main view like this:
take a lock how to remove and release this object from my self.view....
i just have this class:
#interface Post : UIView{
UILabel * label1;
UILabel * label2;
UILabel * label3;
}
#implementation Post
-(void)init
{
self = [super init];
if (self) {
label1 = [[UILabel alloc]init];
label2 = [[UILabel alloc]init];
label3 = [[UILabel alloc]init];
label1.frame = CGRect(10,10,100,30);
label2.frame = CGRect(10,60,100,30);
label3.frame = CGRect(10,110,100,30);
}
return self;
}
#end
and this main class controller
#implementation HomeViewController
-(void)viewDidLoad{
Post *p = [[Post alloc]init]
p.frame = CGRect(0,0,0,320,460);
p.backgroundColor = [UIColor blue];
[self.view addsubview: p];
[p release];
Post *p2 = [[Post alloc]init]
p2.frame = CGRect(0,0,0,320,460);
p2.backgroundColor = [UIColor blue];
[self.view addsubview:p2];
[p2 release];
Post *p3 = [[Post alloc]init]
p3.frame = CGRect(0,0,0,320,460);
p3.backgroundColor = [UIColor blue];
[self.view addsubview:p3];
[p3 release];
}
-(void)RemoveAllPosts
{
//How to remove and release all post from self.view???
}
#end
Please try below lines of code.
for(UIView *viewInner in self.view.subviews) {
if([viewInner isKindOfClass:[Post class]])
[viewInner removeFromSuperview];
}

Scrolling UIScrollView on external screen attached to an iPad

Hey. I've achieved making a programmatic UIScrollView with zooming, but now I've been trying to take the scrollable/zoomable image to an external screen if plugged in.
#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;
}
}
(Sorry I've had no luck getting that formatted to look right on this site)
If a video cable is plugged into an iPad, there's no image on the iPad, which is what I want. The image on the external screen zooms correctly when you do the gesture on the iPad, but I can't figure out how to make it scroll. Thanks in advance.
edit: I now have this -
#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
As I said below, I can now get either scroll or zooming to work separately, but zooming is all messed up if scrolling is working, because when zooming it thinks it's also scrolling. So I want to avoid it scrolling when zooming, and to do this I want to detect the number of touches, which I must be doing wrong!
Got it working with the image being on the iPad and external screen. I'll probably swap it in with a rectangular area because the image is resource heavy to be both the iPad and external screen.
#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
I pressed the space bar (4) times for code, and NOPE doesn't work stack overflow still broken. :)

Resources