I am trying to use an ImageView to be able to display any number between 1 and 6 and be able to display this on a iPhone storyboard but I am having a little difficulty with this. I have using the arc4random() % 6 method to be able to randomize the numbers between 1 and 6, the main problem is being able to display the images that I have uploaded and be able to display this on the simulator. Here is the code I have so far.
This is in the ViewConroller.m
- (IBAction)Button:(UIButton *)sender {
// In the viewDidLoad method:
[_Dice1 sizeToFit];
UIImage *image1 = [UIImage imageNamed:#"dice-1-md.png"];
UIImage *image2 = [UIImage imageNamed:#"dice-2-md.png"];
UIImage *image3 = [UIImage imageNamed:#"dice-3-hi.png"];
UIImage *image4 = [UIImage imageNamed:#"dice-4.png"];
UIImage *image5 = [UIImage imageNamed:#"dice-5-md.png"];
UIImage *image6 = [UIImage imageNamed:#"dice-6-hi.png"];
NSArray*_imageView = [[NSArray alloc]initWithObjects:#[image1, image2, image3, image4, image5, image6], nil];
int dice1 = arc4random() % 6;
if(dice1 == 0){
[_imageView objectAtIndex:0];
}
if(dice1 == 1){
[_imageView objectAtIndex:1];
}
if(dice1 == 2){
[_imageView objectAtIndex:2];
}
if(dice1 == 3){
[_imageView objectAtIndex:3];
}
if(dice1 == 4){
[_imageView objectAtIndex:4];
}
if(dice1 == 5){
[_imageView objectAtIndex:5];
}
}
#end
This is in the ViewController.h
- (IBAction)Button:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UIImageView *Dice1;
Do you guys have any suggestions? Any help would greatly be appreciated. Thanks!
So whats problem? Assign your images to your UIImageView (self.Dice1).
self.Dice1.image=[_imageView objectAtIndex:dice1];
Related
So, I have repeating views containing thumbnails, once a thumbnail is pressed the thumbnail ID is sent as the tag.
With this information I want to get the frame of the subview of that view;
- (IBAction)thumbPressed:(id)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
for (int i = 0; i < _thumbCounter; i++) {
if (i == [sender tag]) {
NSLog(#"thumb view %i", i);
//Up To HERE
break;
}
}
// [self moveToVideoControllerWithInfoID:[NSString stringWithFormat:#"%li", (long)[sender tag]]];
}
For the thumbnails to be drawn they're drawn in a random order retrieved by JSON.
The Button
UIButton *thumbButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thumbButton addTarget:self
action:#selector(thumbPressed:)
forControlEvents:UIControlEventTouchUpInside];
thumbButton.frame = CGRectMake(0, 0, _thumbView.frame.size.width, _thumbView.frame.size.height);
thumbButton.tag = _thumbCounter;
[_thumbView addSubview:thumbButton];
The subview I want to get the frame of
NSString *string = [NSString stringWithFormat:#"***.jpg",Link];
NSURL * imageURL = [NSURL URLWithString:string];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbGap, thumbGap, thumbHeight - 5, thumbHeight - 5)];
imageView.image = image;
[_thumbView addSubview:imageView];
Where the thumbnail shell is drawn
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview is a class of UIVIEW with the added thumbId
How once that button is pressed can I locate the imageView frame inside of _thumbview ( bare in mind there are multiple ).
First off, let's make life easier for you:
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
ThumbView *thumbView = (ThumbView*)[sender superView];
for(UIView* subview in thumbView.subViews) {
if([subView iKindOfClass:[UIImageView class]]) {
//you got it here
}
}
}
You could shortcut the whole thing by making ThumbView have an imageView property as well.
I've tried to determine that this is what you are doing:
_scrollView
->_thumbView[0]
--->thumbButton
--->imageView
->_thumbView[1]
--->thumbButton
--->imageView
...
->_thumbView[N]
--->thumbButton
--->imageView
Assumed that you want the image view who is in the same thumbView as the button who is pressed.
Best Possible Solution (IMHO):
#Interface ThumbView()
#property (nonatomic,weak) UIImageView *imageView;
#end
AND:
(After making sure to first add, and THEN SET .imageView)
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
UIImageView *imageView = ((ThumbView*)[sender superView]).imageView
}
for (UIView *i in _scrollView.subviews) {
if ([i isKindOfClass:[ThumbView class]]) {
ThumbView *tempThumb = (ThumbView *)i;
if (tempThumb.thumbId == (int)[sender tag]) {
for (UIView *y in tempThumb.subviews) {
if ([y isKindOfClass:[UIImageView class]]) {
UIImageView *tempIMG = (UIImageView *)y;
[self playVideo:[loadedInput objectForKey:#"link"] frame:tempIMG.frame andView:tempThumb];
}
}
}
}
}
After build code, it doesn't have any animation on my cellphone
but the code looks good, i don't know how to figure it out ,
and my image is 829 * 445, does it cause this problem ?
Could someone help me to solve this problem i will really appreciate it, thanks
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
imageView.animationImages = animationImages ;
imageView.animationRepeatCount = 2;
imageView.animationDuration= 4.0;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self
selector:#selector(animationDone:)
userInfo:nil repeats:NO];
}
-(void)animationDone:(NSTimer*)inTimer
{
[inTimer invalidate];
inTimer = nil;
NSLog(#"animationDone ");
}
#end
You don't added imageView to your ViewController view.
Try [self.view addSubview:imageView]; in -viewDidLoad
Please use animation block code instead of the older method. The animation will tell you when its done. You don't need to set a timer.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
[UIView animateWithDuration:1.0f animations:^{
for (int loop = 0; loop < 4; loop++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFornmat:#"%d.jpg", (loop + 1)]];
[imageView setImage:image];
}
} completion:^(BOOL finished) {
NSLog(#"animationDone");
}];
It looks like you're not adding the animating UIImageView to your view hierarchy. Right before your [imageView startAnimating]; line add the below line of code:
[self.view addSubview:imageView];
I am trying the following method ,but it isn't work. Is there a way I can check what my current background image is for my UIButton and compare it?
UIImage *buttonCheckImage = checkButton.currentBackgroundImage;
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}
this works using
[ UIImagePNGRepresentation( uiImage1 ) isEqualToData:
UIImagePNGRepresentation( uiImage2 ) ];
UIImage *buttonCheckImage = [self.btn_Checkbox imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkbox"];
if([UIImagePNGRepresentation(buttonCheckImage) isEqualToData:UIImagePNGRepresentation(checkImage)])
{
self.btnImage = [UIImage imageNamed:#"checkbox1"];
}
else
{
self.btnImage = [UIImage imageNamed:#"checkbox"];
}
[self.btn_Checkbox setImage:self.btnImage forState:UIControlStateNormal];
using : http://stackoverflow.com/questions/14078283/comparing-two-images-whether-same-or-not-ios
Hope its working well try this....
.h
#property (weak, nonatomic) IBOutlet UIButton *checkButtonObj;
-(IBAction)buttonClick:(id)sender;
.m
-(void)viewDidLoad {
[super viewDidLoad];
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
-(IBAction)buttonClick:(id)sender {
if ([self.checkButtonObj.imageView.image isEqual:[UIImage imageNamed:#"correct.png"]])
[self.checkButtonObj setImage:[UIImage imageNamed:#"wrong.png"] forState:UIControlStateNormal];
else
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
Try the following code,
UIImage *buttonCheckImage = [checkButton imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}
I am using the below code to change the UIImageView's image after a short duration. The images being used are stored in an array as you can see.
The problem is, no matter what 'duration' or 'delay' I set, the imageview changes almost instantly to the last image in the array.
Should I instead be using the main thread to add a delay between each image transition?
////////////////////////////////////////////////////////////////
animationCount = 0;
imageArray =[NSMutableArray new];
[imageArray addObject:[UIImage imageNamed:#"gauge1final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge2final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge3final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge4final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge5final.png"]];
[self multiStageAnimate];
////////////////////////////////////////////////////////////////
-(void) multiStageAnimate{
[UIView animateWithDuration:5.0
delay:5.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^
{
self.gauge.image = [imageArray objectAtIndex:animationCount];
}
completion:^(BOOL finished){
animationCount ++;
if(animationCount < imageArray.count){
[self multiStageAnimate];
}
else
{
animationCount = 0;
}
}];
}
UIImageView does have a mechanism to accomplish your need.
UIImageView *imageView = [[UIImageView alloc] init];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"], nil];
imageView.animationRepeatCount = 0; // 0 means repeat without stop.
imageView.animationDuration = 1.5; // Animation duration
[imageView startAnimating];
If you are using multiple screens with the same animation, it's better to create one .plist file and then use this anywhere in your project. The steps are as follows:
First, generate one .plist file File-> New File-> Resource ->Property List
Write your code like this:
ViewController.h
#property (weak, nonatomic) IBOutlet UIImageView *dataImageView;
ViewController.m
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"PhotesArray" ofType:#"plist"]];
NSArray *imageNames = [picturesDictionary objectForKey:#"Photos"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i=0; i<[imageNames count]; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
//Normal animation
self.dataImageView.animationImages = images;
self.dataImageView.animationDuration = 5.0;
[self.dataImageView startAnimating];
The image property of a UIImageView won't animate. What you need to do is have 2 UIImageView widgets one on top of another. Then, you can animate their alpha properties from 0 to 1 (and vice-versa) with each transition.
I have this simple function where i sap rate the button animation and then start animation for every button but i dont know why only one button animate not others which clicked, please help me with this
- (IBAction)startAnimation:(UIButton *)button {
NSMutableArray* imagesArray = [[NSMutableArray alloc] init];
for (int images = 0; images < 15; images++) {
UIImage* buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"aaqqq00%02d.png", images]];
[imagesArray addObject:buttonImage];
}
NSArray* reversedAnim = [[imagesArray reverseObjectEnumerator] allObjects];
int buttonTag = button.tag;
animButton.adjustsImageWhenHighlighted = NO;
for (int images = 0; images < 15; images++) {
UIButton *animButton = (UIButton *)[self.view viewWithTag:buttonTag];
if (images <= buttonTag) {
animButton.imageView.animationImages = imagesArray;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0014.png"] forState:UIControlStateNormal];
} else {
animButton.imageView.animationImages = reversedAnim;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0000.png"] forState:UIControlStateSelected];
}
NSLog(#"%#", animButton.imageView.animationImages);
animButton.imageView.animationDuration = 1; //whatever you want (in seconds)
animButton.imageView.animationRepeatCount = 1;
[animButton.imageView startAnimating];
}
}
You're passing the button, so doing the tag lookup seem pointless and error prone (if there are multiple views with the same tag in the hierarchy).
Setting the buttons image view animation images (animButton.imageView.animationImages) and then after that setting a single image ([animButton setImage:...) is also pointless.
What does your log statement say about the animation images?