Making Image Slider on Storyboard Issue - ios

I've made a basic image slider and it is working fine via adding subview's. However I should make it in storyboard. I'm running almost the same code but it is not running.
I just want to make 3 images slider.
So please can anybody tell where would be my issue?
- (void)run{
pageNumber =3;
_scrollview = [[UIScrollView alloc] init];
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
_pgcontrol = [[UIPageControl alloc] init];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = pageNumber;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
UIImageView*im_ = [[UIImageView alloc] init];
im_.contentMode = UIViewContentModeScaleAspectFit;
im_.clipsToBounds = true;
im_.image = [UIImage imageNamed:#"31900.png"];
UIImageView*im1_ = [[UIImageView alloc] init];
im1_.contentMode = UIViewContentModeScaleAspectFit;
im1_.clipsToBounds = true;
im1_.image = [UIImage imageNamed:#"31901.png"];
UIImageView*im2_ = [[UIImageView alloc] init];
im2_.contentMode = UIViewContentModeScaleAspectFit;
im2_.clipsToBounds = true;
im2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:im_];
[_scrollview addSubview:im1_];
[_scrollview addSubview:im2_];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);
if (page_== pageNumber-3) {
NSLog(#"page3");
}
else if (page_ == pageNumber-2){
NSLog(#"page2");
}
else if (page_ == pageNumber-1){
NSLog(#"page3");
}
_pgcontrol.currentPage = page_;
}
/////
Old code
/////
- (void)imageCreater{
self.view.backgroundColor = [UIColor whiteColor];
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect svrect_ = CGRectZero;
svrect_.size.height = self.view.bounds.size.height/3.1*2;
svrect_.size.width = self.view.bounds.size.width/3*2;
CGPoint svcenter_ = CGPointZero;
svcenter_.x = self.view.center.x;
svcenter_.y = self.view.center.y+65;
CGSize svconsize = CGSizeZero;
svconsize.height = svrect_.size.height;
svconsize.width = svrect_.size.width * 3;
CGPoint pgconcenter_ = CGPointZero;
pgconcenter_.x = self.view.center.x;
pgconcenter_.y = svcenter_.y + (svrect_.size.height/2) + 40;
CGRect btnrect_ = CGRectZero;
btnrect_.size.width = 250;
btnrect_.size.height = 50;
CGPoint btncenter_ = CGPointZero;
btncenter_.x = self.view.center.x;
btncenter_.y = self.view.bounds.size.height-65;
_backgroundimageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_backgroundimageview];
_scrollview = [[UIScrollView alloc] initWithFrame:svrect_];
_scrollview.center = svcenter_;
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.contentSize = svconsize;
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
[self.view addSubview:_scrollview];
_pgcontrol = [[UIPageControl alloc] initWithFrame:CGRectZero];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = 3;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
_pgcontrol.center = pgconcenter_;
[self.view addSubview:_pgcontrol];
UIImageView*iv_ = [[UIImageView alloc] initWithFrame:svrect_];
iv_.contentMode = UIViewContentModeScaleAspectFit;
iv_.clipsToBounds = true;
iv_.image = [UIImage imageNamed:#"31900.png"];
svrect_.origin.x += iv_.frame.size.width;
UIImageView*iv1_ = [[UIImageView alloc] initWithFrame:svrect_];
iv1_.contentMode = UIViewContentModeScaleAspectFit;
iv1_.clipsToBounds = true;
iv1_.image = [UIImage imageNamed:#"31901.png"];
svrect_.origin.x += iv1_.frame.size.width;
UIImageView*iv2_ = [[UIImageView alloc] initWithFrame:svrect_];
iv2_.contentMode = UIViewContentModeScaleAspectFit;
iv2_.clipsToBounds = true;
iv2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:iv_];
[_scrollview addSubview:iv1_];
[_scrollview addSubview:iv2_];
}

Try this:
- (void)run
{
// If you are using story board and user interface then you don't need to initialize scroll view.
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
_scrollview.contentSize = CGSizeMake(320*3, _scrollview.frame.size.height);
// Same for Page control you dont need to initialize it
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = pageNumber;
_pgcontrol.currentPage = 0;
UIImageView *im_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*0),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im_.contentMode = UIViewContentModeScaleAspectFit;
im_.clipsToBounds = true;
im_.image = [UIImage imageNamed:#"31900.png"];
UIImageView*im1_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*1),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im1_.contentMode = UIViewContentModeScaleAspectFit;
im1_.clipsToBounds = true;
im1_.image = [UIImage imageNamed:#"31901.png"];
UIImageView*im2_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*2),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im2_.contentMode = UIViewContentModeScaleAspectFit;
im2_.clipsToBounds = true;
im2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:im_];
[_scrollview addSubview:im1_];
[_scrollview addSubview:im2_];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);
if (page_== pageNumber-3)
{
NSLog(#"page3");
}
else if (page_ == pageNumber-2)
{
NSLog(#"page2");
}
else if (page_ == pageNumber-1)
{
NSLog(#"page3");
}
_pgcontrol.currentPage = page_;
}

Related

UITapGestureRecognizer not working, Objective-C

I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;

Image slider pages printing issue?

I'm trying to create image slider view to view 3 pictures as slider. But every time it is creating only one picture with 3 slides. So please where would be my issue?
Should it print an image in each page slide?
- (void)imageCreater{
self.view.backgroundColor = [UIColor whiteColor];
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect svrect_ = CGRectZero;
svrect_.size.height = self.view.bounds.size.height/3*2;
svrect_.size.width = self.view.bounds.size.width/3*2;
CGPoint svcenter_ = CGPointZero;
svcenter_.x = self.view.center.x;
svcenter_.y = self.view.center.y+45;
CGSize svconsize = CGSizeZero;
svconsize.height = svrect_.size.height;
svconsize.width = svrect_.size.width * 3;
CGPoint pgconcenter_ = CGPointZero;
pgconcenter_.x = self.view.center.x;
pgconcenter_.y = svcenter_.y + (svrect_.size.height/2) + 20;
CGRect btnrect_ = CGRectZero;
btnrect_.size.width = 250;
btnrect_.size.height = 50;
CGPoint btncenter_ = CGPointZero;
btncenter_.x = self.view.center.x;
btncenter_.y = self.view.bounds.size.height-65;
_backgroundimageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_backgroundimageview];
_scrollview = [[UIScrollView alloc] initWithFrame:svrect_];
_scrollview.center = svcenter_;
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.contentSize = svconsize;
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
[self.view addSubview:_scrollview];
_pgcontrol = [[UIPageControl alloc] initWithFrame:CGRectZero];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = 3;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
_pgcontrol.center = pgconcenter_;
[self.view addSubview:_pgcontrol];
UIImageView*iv_ = [[UIImageView alloc] initWithFrame:svrect_];
iv_.contentMode = UIViewContentModeScaleAspectFit;
iv_.clipsToBounds = true;
iv_.image = [UIImage imageNamed:#"31902.png"];
UIImageView*iv1_ = [[UIImageView alloc] initWithFrame:svrect_];
iv1_.contentMode = UIViewContentModeScaleAspectFit;
iv1_.clipsToBounds = true;
iv1_.image = [UIImage imageNamed:#"31901.png"];
UIImageView*iv2_ = [[UIImageView alloc] initWithFrame:svrect_];
iv2_.contentMode = UIViewContentModeScaleAspectFit;
iv2_.clipsToBounds = true;
iv2_.image = [UIImage imageNamed:#"31900.png"];
[_scrollview addSubview:iv_];
[_scrollview addSubview:iv1_];
[_scrollview addSubview:iv2_];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);
if (page_== inter-1) {
[_button setTitle:startText forState:UIControlStateNormal];
}else{
[_button setTitle:nextText forState:UIControlStateNormal];
}
_pgcontrol.currentPage = page_;
}
You are setting the same frame to the three imageView
Change the frame as mentioned below, you will get three image in slideView
UIImageView*iv_ = [[UIImageView alloc] initWithFrame:svrect_];
iv_.contentMode = UIViewContentModeScaleAspectFit;
iv_.clipsToBounds = true;
iv_.image = [UIImage imageNamed:#"images-1.jpg"];
svrect_.origin.x += iv_.frame.size.width;
UIImageView*iv1_ = [[UIImageView alloc] initWithFrame:svrect_];
iv1_.contentMode = UIViewContentModeScaleAspectFit;
iv1_.clipsToBounds = true;
iv1_.image = [UIImage imageNamed:#"images-2.jpg"];
svrect_.origin.x += iv1_.frame.size.width;
UIImageView*iv2_ = [[UIImageView alloc] initWithFrame:svrect_];
iv2_.contentMode = UIViewContentModeScaleAspectFit;
iv2_.clipsToBounds = true;
iv2_.image = [UIImage imageNamed:#"images-3.jpg"];
[_scrollview addSubview:iv_];
[_scrollview addSubview:iv1_];
[_scrollview addSubview:iv2_];

Adding a tag to UIImageView

I have an array of an object (which contains an image and some text) and when I iterate around the array and add the image to a UIImageView (which is then added to a UIScrollView) all works well and each image is added with a specific UIImageView to the UIScrollView.
However as soon as I add a tag to the UIImageView not all the images are rendered within the added UIImageView (which is in the UIScrollView (some are missing, some are visible).
If I remove the tag, all works well? Is there something with tags I'm not grasping? Any help much appreciated.
for (Article *al in articleLists){
CGRect frame;
frame.origin.x = self.FeaturedView.frame.size.width * counter;
frame.origin.y = 0;
frame.size = self.FeaturedView.frame.size;
CGRect frameTextArea;
frameTextArea.origin.x = self.FeaturedView.frame.size.width * counter;
frameTextArea.origin.y = (self.FeaturedView.frame.size.height / 2) + 10;
frameTextArea.size.width = self.FeaturedView.frame.size.width;
frameTextArea.size.height = (self.FeaturedView.frame.size.height / 2) - 20;
CGRect frameTextLabel;
frameTextLabel.origin.x = (self.FeaturedView.frame.size.width * counter) + 20;
frameTextLabel.origin.y = (self.FeaturedView.frame.size.height / 2) + 10;
frameTextLabel.size.width = self.FeaturedView.frame.size.width - 40;
frameTextLabel.size.height = (self.FeaturedView.frame.size.height / 2) - 20;
if (al.imageURL != NULL){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
//imageView.tag = counter;
imageView.image = al.imageArticle;
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[imageView addGestureRecognizer:imageTap];
imageView.userInteractionEnabled = YES;
UIView *viewTextArea = [[UIView alloc]initWithFrame:frameTextArea];
viewTextArea.backgroundColor = [UIColor blackColor];
viewTextArea.alpha = 0.9;
UILabel *textLabel = [[UILabel alloc] initWithFrame:frameTextLabel];
textLabel.textColor = [UIColor whiteColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.numberOfLines = 2;
textLabel.font = [UIFont fontWithName:#"Droid Sans" size:18];
textLabel.text = [al.currentTitle uppercaseString];
[self.FeaturedView addSubview:imageView];
[self.FeaturedView addSubview:viewTextArea];
[self.FeaturedView addSubview:textLabel];
}
else{
//UIImage *image = [UIImage imageNamed:#"ArticlePlaceholder.png"];
}
counter++;
}

iOS Image View turn off selection highlighting

Bit of a strange question, so i've attached a screen recording to help...
Video : https://www.dropbox.com/s/3aaefixsk8eejln/Error.mov (See past the watermark!)
My issue is that in my application when the user is at the "Preview" view and is reviewing their images, each time the image is selected, the image receives a touched overlay, which could be confusing for a user as nothing happens when touched.
I would like to disable this if possible..
Below is the example code to how the images are being displayed..
- (void)setReviewImages
{
continueButtonDisabled = YES;
downloadedImageCount = 0;
NSArray *reviewViews = scrollView.subviews;
for (IMReviewView *reviewView in reviewViews) {
[reviewView removeFromSuperview];
}
NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
if ( nPageNum == 0 ){
for (NSUInteger i = 0; i < photoCount; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 65;
frame.size = CGSizeMake(scrollView.frame.size.width, 327.0f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
[scrollView addSubview:subview];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
}
It seems to me like IMReviewView has a UIButton subview. If so, try to set its adjustsImageWhenHighlighted property to NO.

iOS - Reuseable UIView Subclass

I am trying to build a few different UIView subclasses to display information to my app users.
One problem I am having is being able to call the view more than once.
Here is the testing call:
if(rewardsView==nil){
rewardsView = [[RewardsView alloc] init];
[rewardsView showRewardType:RewardsAttack withXP:100 withZBucks:10 isCritical:NO];
}else{
[rewardsView showRewardType:RewardsAttack withXP:200 withZBucks:20 isCritical:NO];
}
Here is the Code to initialize and display:
- (id)init
{
self = [super init];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
//Z-Bucks
zBucksIcon = [UIImage imageNamed:#"rewards-z-bucks"];
zBucksView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[zBucksView setImage:zBucksIcon];
zBucksView.alpha = 0;
zBucksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, zBucksIcon.size.height, zBucksIcon.size.width,25)];
zBucksLabel.backgroundColor = [UIColor clearColor];
zBucksLabel.textAlignment = NSTextAlignmentCenter;
zBucksLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
zBucksLabel.textColor = [UIColor whiteColor];
zBucksLabel.shadowColor = [UIColor blackColor];
zBucksLabel.shadowOffset = CGSizeMake(1, 1);
[zBucksView addSubview:zBucksLabel];
//XP
xpIcon = [UIImage imageNamed:#"rewards-xp"];
xpView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
[xpView setImage:xpIcon];
xpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, xpIcon.size.height, xpIcon.size.width,25)];
xpLabel.backgroundColor = [UIColor clearColor];
xpLabel.textAlignment = NSTextAlignmentCenter;
xpLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
xpLabel.textColor = [UIColor blackColor];
xpLabel.shadowColor = [UIColor whiteColor];
xpLabel.shadowOffset = CGSizeMake(1, 1);
[xpView addSubview:xpLabel];
xpView.alpha = 0;
[self addSubview:zBucksView];
[self addSubview:xpView];
}
return self;
}
-(void)showRewardType:(RewardsType)type withXP:(int)xp withZBucks:(int)zBucks isCritical:(BOOL)critical{
//Set Values
zBucksLabel.text = [NSString stringWithFormat:#"+%i",zBucks];
xpLabel.text = [NSString stringWithFormat:#"+%i",xp];
if(type==RewardsAttack){
}
if(type==RewardsHeal){
}
if(type==RewardsDaily){
}
UIWindow *window = [appDelegate window];
[window addSubview:self];
[self animateRewards];
}
Here is the code that dismisses (after some animation functions are called, not shown here):
-(void)dismissRewards{
[self removeFromSuperview];
//Reset Views
zBucksView.alpha = 0;
xpView.alpha = 0;
[zBucksView setFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[xpView setFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
}

Resources