Scrolling UIScrollView on external screen attached to an iPad - 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. :)

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.

UITableView Layout is Broken

I'm new to iOS development, and now working on an iOS app mainly using UINavigationController and UITableViewController.
The problem I try to solve is the UITableView (Custom tableView cell) layout broken bug on the root viewController. This happens when I back from a child viewController to the root viewController.(Left: Original, Right: Wrong Layout) It looks like UITableView position is moved for some reason, but I'm not sure why.
In MainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
viewFrame = self.view.bounds;
[self loadHeader];
[self loadTable];
self.view.backgroundColor = [UIColor whiteColor];
numberOfCellRow = 20;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)loadView {
[super loadView];
}
- (void)loadHeader {
CGRect headerFrame = viewFrame;
headerFrame.size.width = self.view.frame.size.width;
headerFrame.size.height = 200;
self.headerView = [[HeaderView alloc] initWithFrame:CGRectIntegral(headerFrame)];
[self.view addSubview:self.headerView];
NSLog(#"header loaded");
}
- (void)loadTable {
CGRect tableFrame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y + 150,
viewFrame.size.width, viewFrame.size.height-150);
self.customTableView = [[UITableView alloc] initWithFrame:CGRectIntegral(tableFrame)];
self.customTableView.backgroundColor = [UIColor clearColor];
//set delegates
self.customTableView.delegate = self;
self.customTableView.dataSource = self;
//set custom cell
UINib *nib = [UINib nibWithNibName:#"CardViewCell" bundle:nil];
[self.customTableView registerNib:nib forCellReuseIdentifier:#"Cell"];
self.customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.customTableView];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell loaded");
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData:indexPath];
});
}
- (void)loadData: (NSIndexPath *)indexPath {
_tableCell.titleLabel.text = [[_articles objectAtIndex:indexPath.row] title];
NSLog(#"%#", [[_articles objectAtIndex:indexPath.row] title]);
[_tableCell setNeedsLayout];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
_tableCell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
_tableCell.backgroundColor = [UIColor clearColor];
[_tableCell.contentView setUserInteractionEnabled: NO];
// add touch events
[_tableCell.button addTarget:self
action:#selector(cellPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
NSLog(#"%#", NSStringFromCGRect(tableView.frame));
return _tableCell;
}
In CardViewCell.m
- (void)drawRect:(CGRect)rect {
//self.backgroundColor = [UIColor colorWithRed:228/255.0f green:228/255.0f blue:228/255.0f alpha:1.0f];
NSLog(#"%f", rect.origin.x);
self.backgroundColor = [UIColor clearColor];
[self drawCardView];
[self drawCardImage];
[self drawButton];
[self drawLabels];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)drawCardImage {
_cardViewBounds = _cardView.bounds;
CGRect imageFrame = _cardViewBounds;
imageFrame.size.height = _cardViewBounds.size.height/2+40;
_cardImage = [[UIImageView alloc] initWithFrame:CGRectIntegral(imageFrame)];
_cardImage.image = [UIImage imageNamed:#"sampleImage"];
_cardImage.backgroundColor = [UIColor grayColor];
_cardImage.clipsToBounds = YES;
_cardImage.contentMode = UIViewContentModeScaleAspectFill;
_cardImage.userInteractionEnabled = YES;
_cardImage.tag = 100;
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cardView.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(3.0, 3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.cardView.bounds;
maskLayer.path = maskPath.CGPath;
_cardImage.layer.mask = maskLayer;
[self.cardView addSubview:_cardImage];
}
- (void)drawCardView {
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect frame = CGRectMake(15, 25, (int)screen.size.width-30, 200);
_cardView.layer.cornerRadius = 3;
_cardView.backgroundColor = [UIColor greenColor];
_cardImage.userInteractionEnabled = YES;
_cardImage.tag = 101;
_cardView.frame = CGRectIntegral(frame);
CALayer *caLayer = _cardView.layer;
caLayer.frame = _cardView.frame;
caLayer.shadowRadius = 3.0f;
caLayer.shadowOpacity = 0.4f;
caLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
caLayer.shouldRasterize = YES;
// retina screen resolution
[caLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[caLayer setShouldRasterize:YES];
[self addSubview:_cardView];
}
- (void)drawButton {
CGRect buttonSize = CGRectMake(_cardViewBounds.size.width-100, _cardViewBounds.size.height/2+10, 50, 50);
_button = [[UIButton alloc] initWithFrame:CGRectIntegral(buttonSize)];
_button.layer.cornerRadius = 25;
_button.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
_button.userInteractionEnabled = YES;
_button.tag = 102;
//_button.titleLabel.text = #"+";
[_button setTitle:#"+" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:30.0]];
CALayer *caButtonLayer = _button.layer;
caButtonLayer.frame = _button.frame;
caButtonLayer.shadowRadius = 3.0f;
caButtonLayer.shadowOpacity = 0.2f;
caButtonLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
caButtonLayer.shouldRasterize = YES;
// retina screen resolution
[caButtonLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[caButtonLayer setShouldRasterize:YES];
[self.cardView addSubview:_button];
}
- (void)drawLabels {
CGRect titleLabelSize = CGRectMake( 10, _cardViewBounds.size.height-50, _cardViewBounds.size.width-20, 40);
_titleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(titleLabelSize)];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.numberOfLines = 2;
_titleLabel.userInteractionEnabled = YES;
_titleLabel.tag = 103;
[_titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0]];
[_cardView addSubview:_titleLabel];
[_cardView sendSubviewToBack:_titleLabel];
}
- (void)prepareForReuse {
[super prepareForReuse];
for (UIView *subView in [self.contentView subviews]) {
[subView removeFromSuperview];
}
}
The code of this app is on this link.
Any ideas to solve this?
Your thoughts and help will be hugely appreciated.
the reason the view misplace is due to the _cardView.frame is CGRectZero when disappear, and I didn't find the solve yet.
you can add those to see the change.
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(#"viewWillLayoutSubviews cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(#"viewDidLayoutSubviews cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(#"viewWillDisappear cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(#"viewDidDisappear cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
I think you can learn more about life-cycle about viewController and Cell.
LifeCycle
by the way, when I watching your code, I don't know if you want to use xib or only code to programming. Like the code below, this is init from code but you try to get from xib.
just my opinion.
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// make self from nib file
UINib *nib = [UINib nibWithNibName:#"HeaderView" bundle:nil];
self = [nib instantiateWithOwner:nil options:nil][0];
self.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
CGRect frame = CGRectZero;
frame.size.width = [[UIScreen mainScreen] bounds].size.width;
frame.size.height = 200;
[self setFrame:frame];
}
return self;
}

Tap Gesture does not always work

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.

Animation and ScrollView

I am currently trying to build an image viewer that moves to the next photo every 30 seconds, which also allows to swipe left / right to the next/previous photo manually.
So far I have built two separate apps, one with the swiping functionality and the second with the timer. I have tried many tutorials on the web and am looking for someone to point me in the right direction.
This code switches images every thirty seconds
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *animatedImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 55, 400, 550)];
[self.view addSubview:animatedImageView];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil];
animatedImageView.animationDuration = 30.0 * [animatedImageView.animationImages count];
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
}
This second piece of code has the swiping functionality, I am looking to combine the two
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image1 = [UIImage imageNamed:#"workExample.jpg"];
UIImage *image2 = [UIImage imageNamed:#"IMG_0054.JPG"];
UIImage *image3 = [UIImage imageNamed:#"IMG_0052.JPG"];
imageArray = [NSArray arrayWithObjects:image1, image2, image3, nil];
for (int i = 0; i < [imageArray count]; i++) {
//This is to create a imageview for every single pagecontrol
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [imageArray objectAtIndex:i];
[self.scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}
I am not using a scrollview and implementing in a different way. I hope it suits your needs
In .h
#interface ViewController : UIViewController
{
NSMutableArray *imagesToAnimate;
UIImageView *animatedImageView;
}
#property (strong, nonatomic) IBOutlet UIView *imageGalleryView;
Im viewDidLoad
animatedImageView = [[UIImageView alloc] initWithFrame:self.imageGalleryView.bounds];
imagesToAnimate = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil]];
[self.imageGalleryView addSubview:animatedImageView];
//Start a timer
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(changeImge) userInfo:nil repeats:YES];
animatedImageView.image = imagesToAnimate[0];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(gallerySwiped)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageGalleryView addGestureRecognizer:swipeGesture];
//Give the gallery a border
self.imageGalleryView.layer.borderColor = [UIColor grayColor].CGColor;
self.imageGalleryView.layer.borderWidth = 2.0f;
self.imageGalleryView.layer.cornerRadius = 15.0f;
Now function to update the image as as per scheduled time
-(void)changeImge
{
int index = [imagesToAnimate indexOfObject:animatedImageView.image];
index++;
//this is for the left to right animation ;
CATransition *transition = [CATransition new];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
if (index > imagesToAnimate.count-1) {
animatedImageView.image = imagesToAnimate [0];
}else {
animatedImageView.image = imagesToAnimate[index];
}
[animatedImageView.layer addAnimation:transition forKey:#"transition"];
}
this would handle the swipe
-(void)gallerySwiped
{
[self changeImge];
}
To stop the timer store it in a global variable and call invalidate.I hope this helps.

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];

Resources