I've tried writing this code but I don't think it's correct cause there is an error with the viewDidLoad.
I'm trying to switch between 2 images in the same UIImageView using UISegmentcontol using Storyboard. All the examples online are for switching labels and not using Xcode storyboard.
h.file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *myImage;
IBOutlet UISegmentedControl *segControl;
}
#property(nonatomic, retain)IBOutlet UIImageView *myImage;
#property (nonatomic, retain)IBOutlet UISegmentedControl *segControl;
- (IBAction)switchButton:(id)sender;
#end
m.file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myImage;
#synthesize segControl;
- (IBAction)switchButton:(id)sender
{
if (segControl.selectedSegmentIndex == 0)
{
[myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
else
if (segControl.selectedSegmentIndex == 1)
{
[myImage setImage:[UIImage imageNamed:#"image2.jpg"]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *myImage = [myImage setImage:[UIImage imageNamed:#"image1.jpg"]];
}
If you want to fill the segments of the segmented control with images, you don't need code. Just use the storyboard.
Related
I've got the books, I've checked the apple docs and something is not clicking.
Here's what I want to do: get user values and draw a square/rectangle with them.
A window with one custom view on the right.
On the left two text fields, two labels (width and height) and a button "Draw".
On the right DrawView.
There's how one could do it:
You have to do the connection for the textFields and the button in your appDelegate and also have an IBOutlet for your customView.
In your customView you need to have the two variables which will hold the values from the textField. Don't use property/synthesize because once you set the values you need to call setNeedsDisplay in your customView-DrawView.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "DrawView.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet DrawView *myDrawView;
#property (weak) IBOutlet NSTextField *widthTextField;
#property (weak) IBOutlet NSTextField *heightTextField;
- (IBAction)draw:(id)sender;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "DrawView.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)draw:(id)sender {
[_myDrawView setWidth:[_widthTextField floatValue]];
[_myDrawView setHeight:[_heightTextField floatValue]];
}
DrawView.h
#import <Cocoa/Cocoa.h>
#interface DrawView : NSView {
float width;
float height;
}
-(void) setWidth: (float) aWidth;
-(void) setHeight: (float) aHeight;
#end
DrawView.m
#import "DrawView.h"
#implementation DrawView
#pragma mark - Setters
-(void) setWidht:(float)aWidth {
width = aWidth;
[self setNeedsDisplay:YES];
}
-(void) setHeight:(float)aHeight {
height = aHeight;
[self setNeedsDisplay:YES];
}
#pragma mark - Drawing
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setWidth:10];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
//BackGround Color
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
NSBezierPath* square = [NSBezierPath bezierPath];
[square setLineWidth:2];
NSRect squareRect = NSMakeRect(10,10,width,height);
[square appendBezierPathWithRoundedRect:squareRect xRadius:5 yRadius:5];
[square stroke];
}
#end
That's it. I don't know if there's a better way to do it. It seems a bit bizarre.
Yes it´s nill in drawRect but just before it reaches drawRect it's not because I can calculate the area with the same values...Could you please try it. It takes only 5' to do it.
I have a created a floating button on a UITableViewController. It works like expected.
Now I wanted to use the button as well on a UIViewController where I added a UITableView.
The code for creating the floating button is identically, but the behaviour is different.
At the UITableViewController the button stay where it shall.
At the UIViewController the button moves up (when I scroll down) and down (when I scroll up)
How do I get the button stay where it shall on the UIViewController. Like on the UITableViewController?
TableViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SingletonClass.h"
#interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>
#property (strong, nonatomic) IBOutlet UITableView *magTableView;
#property (strong, nonatomic) UIButton *oButton;
#property (nonatomic) float originalOrigin;
#end
TableViewController.m - floating button, works like expected
- (void)viewDidLoad
{
[super viewDidLoad];
SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
self.originalOrigin = sharedSingleton.photoButtonY; // 430.0
[self addOverlayButton];
}
#pragma mark Camera Button
- (void)addOverlayButton {
self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
[self.oButton setImage:[UIImage imageNamed:#"CameraButton56x56.png"] forState:UIControlStateNormal];
[self.oButton addTarget:self action:#selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:self.oButton aboveSubview:self.view];
}
// to make the button float over the tableView including tableHeader
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds; // should be _magTableView, but works with _tableView as well. But I do not know where this _tableView comes from...
//CGRect tableBounds = self.magTableView.bounds;
// floating Button;
CGRect floatingButtonFrame = self.oButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.oButton.frame = floatingButtonFrame;
[self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
ViewController.h
#import <UIKit/UIKit.h>
#import "SingletonClass.h"
#interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) UIButton *oButton;
#property (nonatomic) float originalOrigin;
#end
ViewController.m - Jumping Button, not wanted behaviour (Update: works no, no jumping anymore)
- (void)viewDidLoad
{
[super viewDidLoad];
self.originalOrigin = [[SingletonClass sharedInstance] photoButtonY]; // 430.0
[self addOverlayButton];
[self initTableView]; // initiate the tableview and works properly
}
#pragma mark Camera Button
- (void)addOverlayButton {
self.oButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.oButton.frame = CGRectMake(132.0, self.originalOrigin, 56.0, 56.0);
[self.oButton setImage:[UIImage imageNamed:#"CameraButton56x56.png"] forState:UIControlStateNormal];
[self.oButton addTarget:self action:#selector(toCameraView:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:self.oButton aboveSubview:self.view];
}
/** Remove this function and the button stayed where I expected it
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect tableBounds = self.tableView.bounds;
// floating Button;
CGRect floatingButtonFrame = self.oButton.frame;
floatingButtonFrame.origin.y = self.originalOrigin + tableBounds.origin.y;
self.oButton.frame = floatingButtonFrame;
[self.view bringSubviewToFront:self.oButton]; // float over the tableHeader
}
*/
I'm building a camera application, and I can't get the zooming on the image to work. I honestly don't know where to go from here, I'm sure it's some dumb mistake that I'm missing though. The app sets the chosenImageView to the workingImage (picture from the camera). When I try to zoom, nothing happens.
ViewController.h
#interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) UIImage *workingImage;
#property (weak, nonatomic) IBOutlet UIImageView *chosenImageView;
#property (weak, nonatomic) IBOutlet UIScrollView *imageScroller;
-(IBAction)cameraButtonPressed;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageScroller.minimumZoomScale = 0.01f;
self.imageScroller.maximumZoomScale = 6.0f;
self.imageScroller.contentSize = self.imageScroller.frame.size;
self.imageScroller.scrollEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.chosenImageView;
}
Here was the problem. You have to control+drag from the UIScrollView to the View Controller to set the delegate.
I'm trying to be able to drag various images around using touches. So far, I'm trying to get it to work with just 1 image, but it's not working (i.e. I can't move the image with my finger). What am I doing wrong?
I have several files with the following code:
DragView.h
#interface DragView : UIImageView {
}
#end
DragView.m
#include "DragView.h"
#implementation DragView
- (void)touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
CGPoint p = [[set anyObject] locationInView:self.superview];
self.center = p;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "DragView.h"
#interface ViewController : UIViewController {
}
#property (nonatomic, strong) DragView *basketView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize basketView;
- (void)viewDidLoad
{
[super viewDidLoad];
basketView = [[DragView alloc]
initWithImage:[UIImage imageNamed:#"basket.png"]];
basketView.frame = CGRectMake(140, 340.2, 60, 30);
[self.view addSubview:basketView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this:
basketView.userInteractionEnabled = YES;
Documentation says that:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
I have view controller, and inside that I have a UIScrollView, for managing the zoom, and a UIImage.
Now I'm handling the zoom effect with UIScrollViewDelegate delegate and method viewForZoomingInScrollView:... but the result is very poor, definitely not fluid!
This is my code:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate, UIActionSheetDelegate>
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.maximumZoomScale = 3.0;
// some code
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
// last update
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if ([self.scrollView zoomScale] < 1.0) {
[self.scrollView setZoomScale:1.0];
}
}
// some other methods...
#end
This is the storyboard view:
I'm not using a Pinch Gesture Recognizer.
All images are quite small like 640x480.
Thanks
P.S. this is a continue of UICollectionView and images.
Checkout out this tutorial I wrote. The first part is exactly what you need to implement.
This has been addressed in other posts:
Zooming image views with autolayout:
Centering logic for autolayout or springs and struts