Stop iCarousel labels from scrolling - ios

All my labels, done button and back button are scrolling within my icarousel. Is there a way to stop them from moving with the swipe gesture or are they inherently fixed? Or is it a case of taking them out of the function all together?
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
GalleryImage* gi = [self.galleryItem.galleryImages objectAtIndex:index];
UIImageView* imageView = nil;
UILabel* positionLabel = nil;
//UILabel* titleLabel = nil;
UILabel* label = nil;
UIButton* doneButton = nil;
UIView* labelBackground = nil;
UIActivityIndicatorView * spinnerView = nil;
UILabel * errorMessage = nil;
UILabel* titleLabel = nil;
//create new view if no view is available for recycling
if (view == nil) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
// Image
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, self.carousel.frame.size.height)] ;
imageView.contentMode = UIViewContentModeScaleAspectFit;// UIViewContentModeCenter;
imageView.tag = 1;
// Spiner
spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinnerView setFrame:imageView.frame];
spinnerView.tag = 11;
//Error message
errorMessage = [[UILabel alloc] initWithFrame:spinnerView.frame];
[errorMessage setTextAlignment:NSTextAlignmentCenter];
[errorMessage setTextColor:[UIColor whiteColor]];
errorMessage.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
errorMessage.tag = 111;
[imageView addSubview:errorMessage];
[imageView addSubview:spinnerView];
[view addSubview:imageView];
// Position
positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)] ;
positionLabel.backgroundColor = [UIColor clearColor];
positionLabel.textAlignment = NSTextAlignmentCenter;
positionLabel.lineBreakMode = NSLineBreakByWordWrapping;
positionLabel.numberOfLines=1;
positionLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 14.0];
positionLabel.textColor = [UIColor whiteColor];
positionLabel.tag = 2;
//if(UIInterfaceOrientationPortrait == self.interfaceOrientation)
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
// Title
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.carousel.frame.size.width - 20, 100)] ;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.numberOfLines = 4;
titleLabel.font = [UIFont fontWithName:#"Georgia" size: 18.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = [NSString stringWithFormat:#"%# \n", self.galleryItem.title];
[view addSubview:titleLabel];
/*
* Manage carousel speed and deceleration rate for portrait mode
*/
//self.carousel.decelerationRate = 0.175f;
//self.carousel.scrollSpeed = 0.45f;
}
else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
/*
* Manage carousel speed and deceleration rate for landscape mode
*/
//self.carousel.decelerationRate = 0.25f;
//self.carousel.scrollSpeed = 0.55f;
}
labelBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.carousel.frame.size.height - 160, self.carousel.frame.size.width , 160)] ;
CAGradientLayer *labelBackgroundGradient = [CAGradientLayer layer];
labelBackgroundGradient.frame = labelBackground.bounds;
labelBackgroundGradient.colors = [NSArray arrayWithObjects: (id)[[UIColor clearColor]CGColor],
(id)[[UIColor blackColor]CGColor],
nil
];
[labelBackground.layer insertSublayer:labelBackgroundGradient atIndex:0];
labelBackground.opaque = false;
labelBackground.tag = 5;
// Description
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake( 0.0f,
0.0f,
labelBackground.bounds.size.width - 20.0f,
labelBackground.bounds.size.height)] ;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.font = [label.font fontWithSize:12];
label.textColor = [UIColor whiteColor];
label.text = gi.descriptionGalleryImage;
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
label.tag = 6;
[labelBackground addSubview:label];
[view addSubview:labelBackground];
} else {
imageView = (UIImageView *)[view viewWithTag:1];
spinnerView = (UIActivityIndicatorView*)[imageView viewWithTag:11];
errorMessage = (UILabel*) [imageView viewWithTag:111];
positionLabel = (UILabel *)[view viewWithTag:2];
doneButton = (UIButton *)[view viewWithTag:3];
//titleLabel = (UILabel *)[view viewWithTag:4];
labelBackground = (UIView *)[view viewWithTag:5];
label = (UILabel*)[view viewWithTag:6];
}
//Start image loading
ImageSize* imSize = [gi getLargestImage];
[spinnerView startAnimating];
errorMessage.text = #"";
[imageView sd_setImageWithURL:[NSURL URLWithString:imSize.source] placeholderImage:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//progress bar could be here
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//remove progressbar
[spinnerView stopAnimating];
//Display error message
if(error)
{
//errorMessage.text = error.localizedDescription;
errorMessage.text = #"Cannot load image";
}
//Assign image
if(image)
{
[imageView setImage:image];
}
}];
positionLabel.text = [NSString stringWithFormat:#"%d of %ld", (index + 1), (long)[self.galleryItem.galleryImages count]];
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.carousel.frame.size.width, 30)];
v.backgroundColor = [UIColor blackColor];
v.alpha = 0.6;
[view addSubview:v];
[view addSubview:positionLabel];
// Done button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0 , 50, 30);
[doneButton setImage:[UIImage imageNamed:#"back_button_white"] forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0,5,0,5);
[doneButton setTintColor:[UIColor whiteColor]];
[doneButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:doneButton];
/*
* The description. Why are we setting the frame for the label multiple times?
* We need to do this so it can autoadjust its size and position according
* to the text contained within it.
*/
[label setText:gi.descriptionGalleryImage];
[label autoAdjustGalleryCaptionSize];
[label setFrame:CGRectMake( label.bounds.origin.x + 10.0f,
label.bounds.origin.y + (labelBackground.bounds.size.height - (label.bounds.size.height + 10.0f)),
label.bounds.size.width,
label.bounds.size.height
)];
labelBackground.hidden = self.hideLandscapeDetails;
label.hidden = self.hideLandscapeDetails;
doneButton.hidden = self.hideLandscapeDetails;
positionLabel.hidden = self.hideLandscapeDetails;
v.hidden =self.hideLandscapeDetails;
titleLabel.hidden = self.hideLandscapeDetails;
self.tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showHideInfo:)];
[view addGestureRecognizer:self.tap1];
// scroll portrait view to same index
if (self.delegate) {
[self.delegate setReturnIndex:self.carousel.currentItemIndex];
}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:#"Image Gallery" // Event category (required)
action:#"Gallery Image Viewed" // Event action (required)
label:#"Picture swipe" // Event label
value:[NSNumber numberWithLong:(index + 1)]] build]]; // Event value
return view;
}

The label should be above the iCarousel to do what you want, not inside the views of the carousel.

Related

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

Variable UITableViewCell height not working

I'm trying to have a UITableView with variable cell heights. The height should be based on the height of an image fetched from the server. I'm having some difficulties though. This is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGFloat height = (0.5*(self.view.frame.size.width-10))+100;
UILabel *heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-35, 30)];
heightLabel.text = [self.streams[indexPath.row] excerpt];
heightLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
heightLabel.numberOfLines = 3;
[heightLabel sizeToFit];
height = height + heightLabel.frame.size.height;
NSLog(#"Cell height is %f", height);
cell.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, cell.frame.size.height-20)];
backgroundView.backgroundColor = [UIColor whiteColor];
backgroundView.tag = 1;
[cell addSubview:backgroundView];
NSURL *imageLink = [NSURL URLWithString:[self.streams[indexPath.row] image]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0.5*(self.view.frame.size.width-10))];
__block UIActivityIndicatorView *activityIndicator;
__weak UIImageView *weakImageView = imageView;
imageView.tag = 2;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[cell addSubview:imageView];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height + 10, self.view.frame.size.width-35, 30)];
nameLabel.text = [self.streams[indexPath.row] name];
nameLabel.textColor = [UIColor blackColor];
nameLabel.textAlignment = NSTextAlignmentLeft;
nameLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:16];
nameLabel.numberOfLines = 2;
[nameLabel sizeToFit];
nameLabel.tag = 3;
[cell addSubview:nameLabel];
UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, nameLabel.frame.origin.y + nameLabel.frame.size.height + 5, self.view.frame.size.width-35, 30)];
descLabel.text = [NSString stringWithFormat:#"Posted by %#", [self.streams[indexPath.row] author]];
descLabel.textColor = [UIColor colorWithRed:193.0/255.0 green:193.0/255.0 blue:193.0/255.0 alpha:1.0];
descLabel.textAlignment = NSTextAlignmentLeft;
descLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:12];
descLabel.numberOfLines = 2;
[descLabel sizeToFit];
descLabel.tag = 5;
[cell addSubview:descLabel];
UILabel *websiteLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, descLabel.frame.origin.y + descLabel.frame.size.height + 5, self.view.frame.size.width-35, 30)];
websiteLabel.text = [self.streams[indexPath.row] excerpt];
websiteLabel.textColor = [UIColor colorWithRed:119.0/255.0 green:119.0/255.0 blue:119.0/255.0 alpha:1.0];
websiteLabel.textAlignment = NSTextAlignmentLeft;
websiteLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
websiteLabel.numberOfLines = 3;
[websiteLabel sizeToFit];
websiteLabel.tag = 4;
[cell addSubview:websiteLabel];
[imageView sd_setImageWithURL: imageLink
placeholderImage:[UIImage imageNamed:#"default.png"]
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (!activityIndicator) {
[weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = weakImageView.center;
[activityIndicator startAnimating];
}
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
float newHeight;
#try {
[activityIndicator removeFromSuperview];
activityIndicator = nil;
float oldWidth = image.size.width;
float scaleFactor = imageView.frame.size.width / oldWidth;
newHeight = image.size.height * scaleFactor;
}
#catch (NSException *exception) {
newHeight = 0.5*(self.view.frame.size.width-10);
}
#finally {
imageView.image = image;
imageView.frame = CGRectMake(10, 10, self.view.frame.size.width-20, newHeight);
nameLabel.frame = CGRectMake(15, imageView.frame.origin.y + newHeight + 10, self.view.frame.size.width-35, 30);
[nameLabel sizeToFit];
descLabel.frame = CGRectMake(15, nameLabel.frame.origin.y + nameLabel.frame.size.height + 5, self.view.frame.size.width-35, 30);
[descLabel sizeToFit];
websiteLabel.frame = CGRectMake(15, descLabel.frame.origin.y + descLabel.frame.size.height + 5, self.view.frame.size.width-35, 30);
[websiteLabel sizeToFit];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self.tableView beginUpdates];
[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, newHeight+nameLabel.frame.size.height+descLabel.frame.size.height+websiteLabel.frame.size.height+50)];
[self.tableView endUpdates];
backgroundView.frame = CGRectMake(10, 10, self.view.frame.size.width-20, newHeight+nameLabel.frame.size.height+descLabel.frame.size.height+websiteLabel.frame.size.height+25);
}
}
}];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0f;
}
What am I doing wrong? Currently, cells are overlapping....
If your cells has variable height you should set AutomaticDimension
in viewDidLoad set this:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
and you can eliminate heightForRowAtIndexPath or set it to automatic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension; //works only about iOS 8
}

ios : how to add uiLabel on top of the background image?

I am trying to add the uiLabel on top of the uiImageVIew which is the background image. When it comes to adding the uiLabel on top of the uiImageView, the uilabel cannot be aded when testing and running. Would you please tell me the alternatives or precautions if doing so ?
The below is my code :
#implementation CoverViewController
- (void)viewDidLoad {
[super viewDidLoad];
_container = [[UIView alloc] initWithFrame:[self.view bounds]];
[self setTapDemo :[UIImage imageNamed:#"cover_1_d.png"] ] ;
}
- (void) setTapDemo: (UIImage *) cover {
_image = [UIImage imageNamed:#"shaded_region.png"];
_imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[_imageShaded setImage:_image];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[imageView setImage: cover ];
_imageShaded.userInteractionEnabled = YES;
UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(touchesBegan:)];
myGesture.numberOfTapsRequired = 1;
myGesture.delegate=self;
[_imageShaded addGestureRecognizer:myGesture];
[_imageShaded setContentMode:UIViewContentModeScaleAspectFill];
NSString * text = #"Quad name:";
CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
fromLabel.text = text;
fromLabel.numberOfLines = 1;
fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[_imageShaded addSubview:fromLabel];
[imageView addSubview:fromLabel];
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
[_container addSubview:fromLabel];
[self.view addSubview:_container];
[self.view sendSubviewToBack:_container];
}
Check This
- (void) setTapDemo: (UIImage *) cover {
_image = [UIImage imageNamed:#"images.jpg"];
_imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
[_imageShaded setImage:_image];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
[imageView setImage: cover ];
_imageShaded.userInteractionEnabled = YES;
UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(touchesBegan:)];
myGesture.numberOfTapsRequired = 1;
myGesture.delegate=self;
[_imageShaded addGestureRecognizer:myGesture];
[_imageShaded setContentMode:UIViewContentModeScaleAspectFill];
NSString * text = #"Quad name:";
// CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 300, 40)];
fromLabel.text = text;
fromLabel.textColor = [UIColor greenColor];
fromLabel.numberOfLines = 1;
fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
[self.view addSubview:_container];
[self.view sendSubviewToBack:_container];
[imageView addSubview:fromLabel];
}
You are adding your UILabel more than once on different views. you should add it on a single view at a time;
//[_imageShaded addSubview:fromLabel]; // 1
//[imageView addSubview:fromLabel]; // 2
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
//[_container addSubview:fromLabel]; //3

Can't remove a label programmatically

I am printing a label programmatically but I can't remove it from the screen. I have tried removeFromSuperview and lbl1.hidden = YES; and lbl1= nil; but non of them work. It stays on the screen all the time while I can see in the debug it pass from ELSE as in the code below.
Where would be my problem?
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Every time you go into reloadData, you are creating a new label, so if you go into reload and jump to the else, you are creating a label, and then removing it.
You need to save that label as an instance variable and remove it/add it in your reloadData.
#property(nonatomic, strong) UILabel *lbl1;
And in your code, do this only ONCE:
self.lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)] autorelease];
And in your reloadData do:
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
self.lbl1.userInteractionEnabled = YES;
//Etc...
}
else{
[self.lbl1 removeFromSuperview];
}
Try like this:
-(void)reloadData
if(!lbl1)
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Try to remove like this....
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[[self.view viewWithTag:30] removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}

iOS iCarousel image offset

Good afternoon. ICarusel I use in my application. I have an image that is loaded as image view. Images are placed exactly in the center. Please tell me how do I move the image up to only 50 pixels in it without moving the rest of the content?
The method for creating views like that:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/Caches"];
NSDictionary *myDic =[magazinesInfo objectAtIndex:index];
//Resize image
UIImage *img = [UIImage imageWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#_img.png",docDir,[myDic objectForKey:#"title"]]] scaledToSize:CGSizeMake(375,510)];
UIImageView *romb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 118, 135)];
UIImageView *faceImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1004)];
UIImage *dwImage = [UIImage imageNamed:#"button.png"];
UIImage *readImage = [UIImage imageNamed:#"read_button.png"];
UIImage *deleteImage = [UIImage imageNamed:#"delete_button.png"];
if(view ==nil)
{
UIButton *myDownloadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[myDownloadButton setBackgroundColor:[UIColor blackColor]];
[myDownloadButton setHidden:YES];
romb.image = [UIImage imageNamed:#"romb.png"];
view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 1004)];
[view setBackgroundColor:[UIColor blackColor]];
view = faceImage;
faceImage.image = nil;
((UIImageView *)view).image = nil;
view.contentMode = UIViewContentModeCenter;
//Magazine number
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(345, 85, 75, 29)];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textAlignment = NSTextAlignmentCenter;
[myLabel setFont:[UIFont fontWithName:#"OpenSans-Light" size:36.0f]];
myLabel.textColor = [UIColor whiteColor];
myLabel.tag = 1;
//Magazine name
nameMag = [[UILabel alloc] initWithFrame:CGRectMake(-65, 450, 100, 100)];
nameMag.backgroundColor = [UIColor yellowColor];
nameMag.textAlignment = NSTextAlignmentCenter;
[nameMag setFont:[UIFont fontWithName:#"OpenSans-Light" size:30.0f]];
nameMag.textColor = [UIColor blackColor];
nameMag.tag = 3;
//Date
dateMag = [[UILabel alloc] initWithFrame:CGRectMake(-67, 500, 500, 30)];
dateMag.backgroundColor = [UIColor greenColor];
dateMag.textAlignment = NSTextAlignmentCenter;
[dateMag setFont:[UIFont fontWithName:#"OpenSans-Light" size:20.0f]];
dateMag.textColor = [UIColor blackColor];
dateMag.tag = 4;
//Download button
downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(350, 700, 128, 37)];
[downloadButton setBackgroundImage:dwImage forState:UIControlStateNormal];
[downloadButton addTarget:self action:#selector(pressDownload:) forControlEvents:UIControlEventTouchUpInside];
downloadButton.tag = 5;
//Read button
readButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 750, 128, 37)];
[readButton setBackgroundImage:readImage forState:UIControlStateNormal];
[readButton addTarget:self action:#selector(readMag:) forControlEvents:UIControlEventTouchUpInside];
readButton.hidden=YES;
readButton.tag = 8;
//Delete button
deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(400, 750, 128, 37)];
[deleteButton setBackgroundImage:deleteImage forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside];
deleteButton.hidden=YES;
deleteButton.tag = 9;
//Progress bar
downloadProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(100, 800, 127, 8)];
downloadPrecent = [[UILabel alloc]initWithFrame:CGRectMake(300, 800, 8, 8)];
downloadPrecent.backgroundColor = [UIColor blueColor];
[downloadPrecent setFont:[UIFont fontWithName:#"OpenSans-Light" size:20.0f]];
dateMag.textColor = [UIColor blackColor];
downloadProgress.tag = 6;
downloadPrecent.tag = 7;
downloadProgress.hidden = YES;
downloadPrecent.hidden = YES;
//Add image subview
[view addSubview:romb];
//Label subview
[view addSubview:myLabel];
[view addSubview:nameMag];
[view addSubview:dateMag];
//Progressbar subview
[view addSubview:downloadProgress];
[view addSubview:downloadPrecent];
//Buttons subview
[view addSubview:downloadButton];
[view addSubview:readButton];
[view addSubview:deleteButton];
[view addSubview:myDownloadButton];
}
else
{
romb.image = (UIImage*)[romb viewWithTag:3];
((UIImageView *)faceImage).image = (UIImage*)[view viewWithTag:2];
myLabel = (UILabel *)[view viewWithTag:1];
nameMag = (UILabel *)[view viewWithTag:3];
dateMag = (UILabel *)[view viewWithTag:4];
downloadProgress = (UIProgressView *) [view viewWithTag:6];
downloadPrecent = (UILabel *)[view viewWithTag:7];
downloadButton = (UIButton *) [view viewWithTag:5];
readButton = (UIButton *) [view viewWithTag:8];
deleteButton = (UIButton*) [view viewWithTag:9];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"index is: %i",index);
NSLog(#"current item index %i",[self.carousel currentItemIndex]);
NSLog(#"%hhd",[fileManager fileExistsAtPath:[NSString stringWithFormat:#"%#/%#_mag.pdf",docDir,[myDic objectForKey:#"title"]]]);
if ([fileManager fileExistsAtPath:[NSString stringWithFormat:#"%#/%#_mag.pdf",docDir,[myDic objectForKey:#"title"]]] == YES && (index == [self.carousel currentItemIndex]))
{
NSLog(#"%#",[[view subviews] objectAtIndex:9]);
[[[view subviews] objectAtIndex:9] setHidden:NO];
readButton.hidden = NO;
deleteButton.hidden = NO;
downloadButton.hidden = YES;
}
else
{
[[[view subviews] objectAtIndex:9] setHidden:YES];
readButton.hidden = YES;
deleteButton.hidden = YES;
downloadButton.hidden = NO;
}
((UIImageView *)view).image = img;
myLabel.text = [myDic objectForKey:#"title"];
dateMag.text = [myDic objectForKey:#"date"];
romb.image = [UIImage imageNamed:#"romb.png"];
return view;
}
Now it looks like this (do not worry, it's still developing :))
http://files.mail.ru/B2B41BA915624173B646184D4283D9F9?t=1
Use the contentOffset property to shift the carousel contents relative to the carousel view without changing their perspective.
You can use the viewpointOffset propert instead if you want it to look like the perspective has changed (i.e as if you are looking at the carousel from below).

Resources