I just want in my application a ticker,
i have no idea to implement ticker please tell me.
Thanks
Everything you need to do this is in the SDK, no need to customize at all. I haven't checked this, but you can try the following:
#import <UIKit/UIKit.h>
#interface TickerScrollView : UIScrollView {
UILabel *textLabel;
}
- (void)displayText:(NSString *)string;
- (void)clearTicker;
#property (nonatomic, retain, readwrite) UILabel *textLabel;
#end
////
#import "TickerScrollView.h"
#interface TickerScrollView()
- (void)initialiseTextLabel;
- (void)clearTicker;
- (void)beginAnimation;
#end
#implementation TickerScrollView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self setFrame: frame];
[self setBounces: NO];
[self setUserInteractionEnabled:NO];
[self setShowsVerticalScrollIndicator:NO];
[self setShowsHorizontalScrollIndicator:NO];
[self initialiseTextLabel];
}
return self;
}
- (void)initialiseTextLabel {
textLabel = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
[textLabel setTextAlignment:UITextAlignmentLeft];
[textLabel setNumberOfLines:1];
[textLabel sizeToFit];
[self addSubview:textLabel];
[self sendSubviewToBack:textLabel];
[self setScrollEnabled:YES];
}
- (void)displayText:(NSString *)string {
[self clearTicker];
[textLabel setText:string];
[textLabel sizeToFit];
[self setContentSize:textLabel.frame.size];
[self beginAnimation];
}
- (void)clearTicker {
[textLabel setText:#""];
[textLabel sizeToFit];
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
}
- (void)beginAnimation {
CGFloat text_width = textLabel.frame.size.width;
CGFloat display_width = self.frame.size.width;
if ( text_width > display_width ) {
CGPoint origin = CGPointMake(0, 0);
[self setContentOffset:origin];
CGPoint terminal_origin = CGPointMake(textLabel.frame.size.width - self.frame.size.width, textLabel.frame.origin.y);
float duration = (text_width - display_width)/50;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:duration];
[self setContentOffset:terminal_origin];
[UIView commitAnimations];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)dealloc {
[textLabel release];
[super dealloc];
}
#synthesize textLabel;
#end
Assuming by "Ticker" you mean a horizontally scrolling text:
A ticker is basically just a text string that is moving by having its x coordinate changed continuously. Check this simple tutorial on how to display a label:
http://knol.google.com/k/iphone-sdk-helloworld
Then later you can animate it by using an NSTimer to call a method updating the labels x coordinate continuously.
Related
I am getting an error in my project. I have tried plenty of solutions but to no avail.
I am getting an error Xcode Error Semantic error value may not respond to 'initWithFrame:image Name:'
I have no idea what this means and why i'm getting this warning. Please help.
Thanks
Link to my project and the Updated project link.
I'm getting the error in this line
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName];
GalleryScrollView.h
#import <UIKit/UIKit.h>
#import "AttachmentItem.h"
#import "GalleryButton.h"
#protocol GAlleryScrollDelegate;
#interface GalleryScrollView : UIView <GalleryButtonDelegate>
{
id <GAlleryScrollDelegate> delegate;
// MAIN WINDOW WHERE YOU CAN DRAG ICONS
UIView *mainView;
UIScrollView *_scrollView;
NSMutableArray *_attachments;
NSInteger *_totalSize;
UIImageView *_recycleBin;
CGRect recycleBinFrame;
}
#property (nonatomic, retain) id <GAlleryScrollDelegate> delegate;
#property (nonatomic, retain) UIView *mainView;
#property (nonatomic, retain) NSMutableArray *attachments;
#property (nonatomic, retain) UIImageView *recycleBin;
#property (nonatomic, retain) UIImageView *imgName;
#property (nonatomic) CGRect recycleBinFrame;
- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName;
- (void) removeAttachment:(GalleryButton *)button;
- (void) reloadData;
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName;
#end
// EVENTS IF YOU WANT TO DISABLE SOME SCROLL ON DID PRESS AND ENABLE IT ON DROP
#protocol GAlleryScrollDelegate
- (void) didPressButton;
- (void) didDropButton;
#end
GalleryScrollView.m
#import <QuartzCore/QuartzCore.h>
#import "GalleryScrollView.h"
#import "GalleryButton.h"
#implementation GalleryScrollView
#synthesize delegate;
#synthesize mainView;
#synthesize attachments = _attachments;
#synthesize recycleBin = _recycleBin, recycleBinFrame;
int padding = 0;
#pragma mark - INIT
- (id) init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName
{
self = [super initWithFrame:frame];
if (self){
;
}
return self;
}
- (void) awakeFromNib
{
// INIT ATTACHMENT ARRAY
if (_attachments == nil){
_attachments = [[NSMutableArray alloc] init];
}
// SCROLL VIEW
UIScrollView *scrollTemp = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-0, 450)];
_scrollView = scrollTemp;
_scrollView.backgroundColor = [UIColor clearColor];
// RECYCLE BIN
UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mozambique-wenge.png"]];
self.recycleBin = imageViewTemp;
self.recycleBin.frame = CGRectMake(0, 0, 320, 270);
[self addSubview:_scrollView];
[self addSubview:self.recycleBin];
[scrollTemp release];
[imageViewTemp release];
}
- (void) dealloc {
[super dealloc];
}
#pragma mark - ATTACHMENTS ADD / REMOVE
- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName
{
// SAVE ATTACHMENT
[_attachments addObject:attachment];
// RESIZE CONTENT VIEW FOR INSERTINT NEW ATTACHMENT
_scrollView.contentSize = CGSizeMake([_attachments count]*70, 70);
CGFloat startX = (70.0f * ((float)[_attachments count] - 1.0f) + padding);
CGFloat startY = 370;
CGFloat width = 64;
CGFloat height = 64;
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName];
btnAttachment.tag = [_attachments count];
btnAttachment.scrollParent = _scrollView;
btnAttachment.mainView = self.mainView;
btnAttachment.delegate = self;
if (attachment.type == 1){
}else if (attachment.type == 2){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
imageView.image=[UIImage imageNamed:#"mozambique-wenge"];
[btnAttachment addSubview:imageView];
[imageView release];
} else if (attachment.type == 3){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
imageView.image=[UIImage imageNamed:#"recyclebin.png"];
[btnAttachment addSubview:imageView];
[imageView release];
}
[_scrollView addSubview:btnAttachment];
[btnAttachment release];
}
- (void) removeAttachment:(GalleryButton *)button
{
}
#pragma mark - RELOAD DATA
- (void) reloadData
{
}
#pragma mark - GALLERY BUTTON DELEGATE
-(void) touchDown
{
[self.delegate didPressButton];
}
-(void) touchUp
{
[self.delegate didDropButton];
_scrollView.scrollEnabled = YES;
}
-(BOOL) isInsideRecycleBin:(GalleryButton *)button touching:(BOOL)finished;
{
CGPoint newLoc = [self convertPoint:self.recycleBin.frame.origin toView:self.mainView];
CGRect binFrame = self.recycleBin.frame;
binFrame.origin = newLoc;
if (CGRectIntersectsRect(binFrame, button.frame) == TRUE){
if (finished){
[self removeAttachment:button];
}
return YES;
}
else {
return NO;
}
}
#end
GalleryButton.h
#import <UIKit/UIKit.h>
#protocol GalleryButtonDelegate;
#interface GalleryButton : UIView
{
id<GalleryButtonDelegate> delegate;
CGPoint _originalPosition;
CGPoint _originalOutsidePosition;
BOOL isInScrollview;
// PARENT VIEW WHERE THE VIEWS CAN BE DRAGGED
UIView *mainView;
// SCROLL VIEW WHERE YOU GONNA PUT THE THUMBNAILS
UIScrollView *scrollParent;
UIImageView *images;
}
#property (nonatomic, retain) id<GalleryButtonDelegate> delegate;
#property (nonatomic) CGPoint originalPosition;
#property (nonatomic, retain) UIView *mainView;
#property (nonatomic, retain) UIScrollView *scrollParent;
#property (nonatomic, retain) IBOutlet UIImageView *images;
#end
#protocol GalleryButtonDelegate
-(void) touchDown;
-(void) touchUp;
-(BOOL) isInsideRecycleBin:(GalleryButton *)button touching:(BOOL)finished;
#end
GalleryButton.m
#import <QuartzCore/QuartzCore.h>
#import "GalleryButton.h"
#import "GalleryScrollView.h"
#import "AttachmentItem.h"
#implementation GalleryButton
#synthesize delegate;
#synthesize originalPosition = _originalPosition;
#synthesize mainView, scrollParent;
#synthesize images;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self){
isInScrollview = YES;
self.backgroundColor = [UIColor blackColor];
self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5;
}
return self;
}
#pragma mark - DRAG AND DROP
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate touchDown];
self.originalPosition = self.center;
self.scrollParent.scrollEnabled = NO;
if (isInScrollview == YES) {
CGPoint newLoc = CGPointZero;
newLoc = [[self superview] convertPoint:self.center toView:self.mainView];
_originalOutsidePosition = newLoc;
// [self.superview touchesCancelled:touches withEvent:event];
[self removeFromSuperview];
self.center = newLoc;
[self.mainView addSubview:self];
[self.mainView bringSubviewToFront:self];
isInScrollview = NO;
}
else {
;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView beginAnimations:#"stalk" context:nil];
[UIView setAnimationDuration:.001];
[UIView setAnimationBeginsFromCurrentState:YES];
UITouch *touch = [touches anyObject];
self.center = [touch locationInView: self.superview];
[UIView commitAnimations];
if ([delegate isInsideRecycleBin:self touching:NO]){
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([delegate isInsideRecycleBin:self touching:YES]){
CGRect myImageRect = CGRectMake(0, 0, 320, 300);
images = [[UIImageView alloc] initWithFrame:myImageRect];
[images setImage:[UIImage imageNamed:#"light-cherry.png"]];
[self.mainView addSubview:images];
[self.images viewWithTag:1];
UIImageView * animation = [[UIImageView alloc] init];
animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40);
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"iconEliminateItem1.png"],
[UIImage imageNamed: #"iconEliminateItem2.png"],
[UIImage imageNamed: #"iconEliminateItem3.png"],
[UIImage imageNamed: #"iconEliminateItem4.png"]
,nil];
[animation setAnimationRepeatCount:1];
[animation setAnimationDuration:0.35];
[animation startAnimating];
[self.mainView addSubview:animation];
[animation bringSubviewToFront:self.mainView];
[animation release];
;
[UIView beginAnimations:#"goback" context:nil];
[UIView setAnimationDuration:0.4f];
[UIView setAnimationBeginsFromCurrentState:YES];
self.center = _originalOutsidePosition;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop:finished:context:)];
// loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
[UIView commitAnimations];
} else{
[UIView beginAnimations:#"goback" context:nil];
[UIView setAnimationDuration:0.4f];
[UIView setAnimationBeginsFromCurrentState:YES];
self.center = _originalOutsidePosition;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop:finished:context:)];
// loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
[UIView commitAnimations];
}
[self.delegate touchUp];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:#"goback"] && finished) {
[self removeFromSuperview];
self.center = _originalPosition;
[self.scrollParent addSubview:self];
isInScrollview = YES;
}
}
#end
The problem is that you haven't declared the custom init method:
- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imageName;
In GalleryButton.h. You also haven't implemented it in GalleryButton.m. Perhaps you didn't mean to use this method at all in your code?
Either way you've referenced it and Xcode has correctly warned you that it doesn't exist (if you build and run the code you will get an Unrecognized selector sent to instance exception).
You need to place initWithFrame:image Name in GalleryButton in its header file. Always remember, if you get a warning that's says something like "May not respond to..." It's because it's not publicly mentioned in the classes header file.
Okay. After killing myself trying I found my mistake.
I Used:
- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imageName;
In both the GalleryButton.h and GalleryScrollview.h and I just took it out of GalleryScrollview.h
Thanks All
I have a question. I want to display the user some content in a UIScrollView. I want to autoscroll the UIScrollView fast from bottom to top (like in the apple stores iPad). I tried to use DDAutoscrollview (If someone knows), but it doesn't work for me. Do have someone a solution for me to autoscroll a UIScrollView? Any code snippets would be nice.
.h
#interface Interface1 : UIViewController {
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *warnung;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x,
self.scrollView.contentSize.height -
self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
- (void)viewDidLoad {
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 420)];
[super viewDidLoad];
}
Thanks.
> THUMBS UP FOR THE AWNSER THAT WAS GIVEN BY TOBI!!!
Just use setContentOffset:animated:
UIScrollView *scrollView = ...;
CGPoint newOffset = scrollView.contentOffset;
newOffset.y = 0;
[scrollView setContentOffset:newOffset animated:YES];
Edit:
To use it like some kind of start animation you could do this in the scrollView's view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
Edit 2 / 3:
To make the scrolling happen slower, use this:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
float scrollDuration = 4.0;
[UIView animateWithDuration:scrollDuration animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}];
}
I have made a little test to drag views in a UIScrollView to UIView where I can call any action.
I'm trying to be able to make the image I made in the UIScrollView Draggable. I have not been able to make this work.
I was able to create test views which are not images that are able to drag.
TestViewController.h
#import "JDDroppableView.h"
#interface TestViewController : UIViewController <JDDroppableViewDelegate>
{
UIScrollView* mScrollView;
UIView* mDropTarget;
UIImageView *images;
UIImageView *image;
CGPoint mLastPosition;
}
- (void) relayout;
- (void) addView: (id) sender;
- (void) scrollToBottomAnimated: (BOOL) animated;
#property (nonatomic, retain) IBOutlet UIImageView *images;
#property (nonatomic, retain) IBOutlet UIImageView *image;
#end
TestViewController.m
#import "TestViewController.h"
#import "JDDroppableView.h"
#import <QuartzCore/QuartzCore.h>
// setup view vars
static NSInteger sDROPVIEW_MARGIN = 3;
static CGFloat sCOUNT_OF_VIEWS_HORICONTALLY = 1.0;
static CGFloat sCOUNT_OF_VIEWS_VERTICALLY = 1.0;
#implementation TestViewController
#synthesize images;
#synthesize image;
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// increase viewcount on ipad
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
sCOUNT_OF_VIEWS_HORICONTALLY = 0;
sCOUNT_OF_VIEWS_VERTICALLY = 0;
}
// add button
UIButton* button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"002.jpg"] forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[button setTitle: #"+" forState: UIControlStateNormal];
[button addTarget: self action: #selector(addView:) forControlEvents: UIControlEventTouchUpInside];
button.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.2 blue: 0 alpha: 1.0];
button.layer.cornerRadius = 5.0;
button.showsTouchWhenHighlighted = YES;
button.adjustsImageWhenHighlighted = YES;
button.frame = CGRectMake(20,
self.view.frame.size.height - 52,
self.view.frame.size.width - 40, // width
32); // height
[self.view addSubview: button];
// drop target
mDropTarget = [[UIView alloc] init];
mDropTarget.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
mDropTarget.backgroundColor = [UIColor orangeColor];
mDropTarget.frame = CGRectMake(0, 0, 30, 30);
mDropTarget.center = CGPointMake(self.view.frame.size.width/2, button.frame.origin.y - 50);
mDropTarget.layer.cornerRadius = 15;
[self.view addSubview: mDropTarget];
[mDropTarget release];
// scrollview
mScrollView = [[UIScrollView alloc] init];
mScrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
mScrollView.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.2 blue: 0 alpha: 1.0];
mScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
mScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 5, 5, 5);
mScrollView.contentInset = UIEdgeInsetsMake(6, 6, 6, 6);
mScrollView.layer.cornerRadius = 5.0;
mScrollView.frame = CGRectMake(20,20, self.view.frame.size.width - 40, mDropTarget.center.y - 70);
mScrollView.userInteractionEnabled = NO;
mScrollView.canCancelContentTouches = NO;
[self.view addSubview: mScrollView];
[mScrollView release];
// animate some draggable views in
int numberOfViews = sCOUNT_OF_VIEWS_HORICONTALLY*floor(sCOUNT_OF_VIEWS_VERTICALLY) + 2;
CGFloat animationTimePerView = 0.15;
for (int i = 0; i < numberOfViews; i++) {
[self performSelector: #selector(addView:) withObject: nil afterDelay: i*animationTimePerView];
if (i%(int)sCOUNT_OF_VIEWS_HORICONTALLY==0) {
[self performSelector: #selector(scrollToBottomAnimated:) withObject: [NSNumber numberWithBool: YES] afterDelay: i*animationTimePerView];
}
}
CGRect myImageRect = CGRectMake(0, 0, 320, 100);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"002.jpg"]];
[image setUserInteractionEnabled:YES];
[self.view addSubview:image];
[mScrollView addSubview:image];
// reenable userinteraction after animation ended
[mScrollView performSelector: #selector(setUserInteractionEnabled:) withObject: [NSNumber numberWithBool: YES] afterDelay: numberOfViews*animationTimePerView];
}
#pragma layout
- (void) relayout
{
// cancel all animations
[mScrollView.layer removeAllAnimations];
for (UIView* subview in mScrollView.subviews)
[subview.layer removeAllAnimations];
// setup new animation
[UIView beginAnimations: #"drag" context: nil];
// init calculation vars
float posx = 0;
float posy = 0;
CGRect frame = CGRectZero;
mLastPosition = CGPointMake(0, -100);
CGFloat contentWidth = mScrollView.contentSize.width - mScrollView.contentInset.left - mScrollView.contentInset.right;
// iterate through all subviews
for (UIView* subview in mScrollView.subviews)
{
// ignore scroll indicators
if (!subview.userInteractionEnabled) {
continue;
}
// create new position
frame = subview.frame;
frame.origin.x = posx;
frame.origin.y = posy;
// update frame (if it did change)
if (frame.origin.x != subview.frame.origin.x ||
frame.origin.y != subview.frame.origin.y) {
subview.frame = frame;
}
// save last position
mLastPosition = CGPointMake(posx, posy);
// add size and margin
posx += frame.size.width + sDROPVIEW_MARGIN;
// goto next row if needed
if (posx > mScrollView.frame.size.width - mScrollView.contentInset.left - mScrollView.contentInset.right)
{
posx = 0;
posy += frame.size.height + sDROPVIEW_MARGIN;
}
}
// fix last posy for correct contentSize
if (posx != 0) {
posy += frame.size.height;
} else {
posy -= sDROPVIEW_MARGIN;
}
// update content size
mScrollView.contentSize = CGSizeMake(contentWidth, posy);
[UIView commitAnimations];
}
- (void) addView: (id) sender
{
CGFloat contentWidth = mScrollView.frame.size.width - mScrollView.contentInset.left - mScrollView.contentInset.right;
CGFloat contentHeight = mScrollView.frame.size.height - mScrollView.contentInset.top;
CGSize size = CGSizeMake(((contentWidth-sDROPVIEW_MARGIN*(sCOUNT_OF_VIEWS_HORICONTALLY-1))/sCOUNT_OF_VIEWS_HORICONTALLY),
floor((contentHeight-sDROPVIEW_MARGIN*(sCOUNT_OF_VIEWS_VERTICALLY-1))/sCOUNT_OF_VIEWS_VERTICALLY));
JDDroppableView * dropview = [[JDDroppableView alloc] initWithDropTarget: mDropTarget];
dropview.backgroundColor = [UIColor blackColor];
dropview.layer.cornerRadius = 3.0;
dropview.frame = CGRectMake(mLastPosition.x, mLastPosition.y, size.width, size.height);
dropview.delegate = self;
[mScrollView addSubview: dropview];
[dropview release];
[self relayout];
// scroll to bottom, if added manually
if ([sender isKindOfClass: [UIButton class]]) {
[self scrollToBottomAnimated: YES];
}
}
- (void) scrollToBottomAnimated: (BOOL) animated
{
[mScrollView.layer removeAllAnimations];
CGFloat bottomScrollPosition = mScrollView.contentSize.height;
bottomScrollPosition -= mScrollView.frame.size.height;
bottomScrollPosition += mScrollView.contentInset.top;
bottomScrollPosition = MAX(-mScrollView.contentInset.top,bottomScrollPosition);
CGPoint newOffset = CGPointMake(-mScrollView.contentInset.left, bottomScrollPosition);
if (newOffset.y != mScrollView.contentOffset.y) {
[mScrollView setContentOffset: newOffset animated: animated];
}
}
#pragma -
#pragma droppabe view delegate
- (BOOL) shouldAnimateDroppableViewBack: (JDDroppableView *)view wasDroppedOnTarget: (UIView *)target
{
[self droppableView: view leftTarget: target];
CGRect frame = view.frame;
frame.size.width *= 0.3;
frame.size.height *= 0.3;
frame.origin.x += (view.frame.size.width-frame.size.width)/2;
frame.origin.y += (view.frame.size.height-frame.size.height)/2;
[UIView beginAnimations: #"drag" context: nil];
[UIView setAnimationDelegate: view];
[UIView setAnimationDidStopSelector: #selector(removeFromSuperview)];
view.frame = frame;
view.center = target.center;
[UIView commitAnimations];
[self relayout];
[mScrollView flashScrollIndicators];
return NO;
}
- (void) droppableViewBeganDragging:(JDDroppableView *)view
{
[UIView beginAnimations: #"drag" context: nil];
view.backgroundColor = [UIColor colorWithRed: 1 green: 0.5 blue: 0 alpha: 1];
view.alpha = 0.8;
[UIView commitAnimations];
}
- (void) droppableView:(JDDroppableView *)view enteredTarget:(UIView *)target
{
target.transform = CGAffineTransformMakeScale(1.5, 1.5);
target.backgroundColor = [UIColor greenColor];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"image Collided"
// message:#"FuckYea"
// delegate:nil
// cancelButtonTitle:#"Reset!"
// otherButtonTitles:nil];
//[alert show];
CGRect myImageRect = CGRectMake(0, 0, 320, 100);
images = [[UIImageView alloc] initWithFrame:myImageRect];
[images setImage:[UIImage imageNamed:#"002.jpg"]];
[self.view addSubview:images];
}
- (void) droppableView:(JDDroppableView *)view leftTarget:(UIView *)target
{
target.transform = CGAffineTransformMakeScale(1.0, 1.0);
target.backgroundColor = [UIColor orangeColor];
}
- (void) droppableViewEndedDragging:(JDDroppableView *)view
{
[UIView beginAnimations: #"drag" context: nil];
view.backgroundColor = [UIColor blackColor];
view.alpha = 1.0;
[UIView commitAnimations];
}
#end
JDDroppableView.h
#class JDDroppableView;
#protocol JDDroppableViewDelegate <NSObject>
#optional
- (void) droppableViewBeganDragging: (JDDroppableView*) view;
- (void) droppableViewDidMove: (JDDroppableView*) view;
- (void) droppableView: (JDDroppableView*) view enteredTarget: (UIView*) target;
- (void) droppableView: (JDDroppableView*) view leftTarget: (UIView*) target;
- (BOOL) shouldAnimateDroppableViewBack: (JDDroppableView*) view wasDroppedOnTarget: (UIView*) target;
- (void) droppableViewEndedDragging: (JDDroppableView*) view;
#end
#interface JDDroppableView : UIView
{
UIView * mDropTarget;
UIView * mOuterView;
UIScrollView * mScrollView;
BOOL mIsDragging;
BOOL mIsOverTarget;
CGPoint mOriginalPosition;
}
#property (nonatomic, assign) id<JDDroppableViewDelegate> delegate;
- (id) initWithFrame:(CGRect)frame;
- (id) initWithDropTarget:(UIView*)target;
#end
JDDroppableView.m
#import "JDDroppableView.h"
#interface JDDroppableView (hidden)
- (void) beginDrag;
- (void) dragAtPosition: (UITouch *) touch;
- (void) endDrag;
- (void) changeSuperView;
- (BOOL) handleDroppedView;
#end
#implementation JDDroppableView
#synthesize delegate = _delegate;
- (id)init
{
return [self initWithFrame: [UIApplication sharedApplication].keyWindow.frame];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
mIsDragging = NO;
mIsOverTarget = NO;
}
return self;
}
- (id) initWithDropTarget: (UIView *) target;
{
self = [self init];
if (self != nil) {
mDropTarget = target;
}
return self;
}
#pragma mark touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self beginDrag];
[self dragAtPosition: [touches anyObject]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dragAtPosition: [touches anyObject]];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self endDrag];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self endDrag];
}
#pragma mark dragging logic
- (void) beginDrag
{
mIsDragging = YES;
if ([_delegate respondsToSelector: #selector(droppableViewBeganDragging:)]) {
[_delegate droppableViewBeganDragging: self];
};
mOriginalPosition = self.center;
[self changeSuperView];
}
- (void) dragAtPosition: (UITouch *) touch
{
[UIView beginAnimations: #"drag" context: nil];
self.center = [touch locationInView: self.superview];
[UIView commitAnimations];
if ([_delegate respondsToSelector: #selector(droppableViewDidMove:)]) {
[_delegate droppableViewDidMove:self];
}
if (mDropTarget) {
CGRect intersect = CGRectIntersection(self.frame, mDropTarget.frame);
if (intersect.size.width > 10 || intersect.size.height > 10)
{
if (!mIsOverTarget)
{
mIsOverTarget = YES;
if ([_delegate respondsToSelector: #selector(droppableView:enteredTarget:)]) {
[_delegate droppableView: self enteredTarget: mDropTarget];
}
}
}
else if (mIsOverTarget)
{
mIsOverTarget = NO;
if ([_delegate respondsToSelector: #selector(droppableView:leftTarget:)]) {
[_delegate droppableView: self leftTarget: mDropTarget];
}
}
}
}
- (void) endDrag
{
mIsOverTarget = NO;
if([_delegate respondsToSelector: #selector(droppableViewEndedDragging:)]) {
[_delegate droppableViewEndedDragging: self];
}
if (mDropTarget) {
CGRect intersect = CGRectIntersection(self.frame, mDropTarget.frame);
if (intersect.size.width > 10 || intersect.size.height > 10) {
if([self handleDroppedView]) {
mIsDragging = NO;
return;
}
}
}
[self changeSuperView];
mIsDragging = NO; // this needs to be after superview change
[UIView beginAnimations: #"drag" context: nil];
self.center = mOriginalPosition;
[UIView commitAnimations];
}
- (BOOL) handleDroppedView
{
if (mDropTarget && [_delegate respondsToSelector: #selector(shouldAnimateDroppableViewBack:wasDroppedOnTarget:)]) {
return ![_delegate shouldAnimateDroppableViewBack: self wasDroppedOnTarget: mDropTarget];
}
return NO;
}
#pragma mark superview handling
- (void)willMoveToSuperview:(id)newSuperview
{
if (!mIsDragging && [newSuperview isKindOfClass: [UIScrollView class]]) {
mScrollView = newSuperview;
mOuterView = mScrollView.superview;
}
}
- (void) changeSuperView
{
if (!mScrollView) {
[self.superview bringSubviewToFront: self];
return;
}
UIView * tmp = self.superview;
[self removeFromSuperview];
[mOuterView addSubview: self];
mOuterView = tmp;
// set new position
CGPoint ctr = self.center;
if (mOuterView == mScrollView) {
ctr.x += mScrollView.frame.origin.x - mScrollView.contentOffset.x;
ctr.y += mScrollView.frame.origin.y - mScrollView.contentOffset.y;
} else {
ctr.x -= mScrollView.frame.origin.x - mScrollView.contentOffset.x;
ctr.y -= mScrollView.frame.origin.y - mScrollView.contentOffset.y;
}
self.center = ctr;
}
#end
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow* mWindow;
TestViewController* mViewController;
}
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
mWindow = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
mWindow.backgroundColor = [UIColor whiteColor];
TestViewController* viewController = [[TestViewController alloc] init];
[mWindow addSubview: viewController.view];
[mWindow makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[mWindow release];
[mViewController release];
[super dealloc];
}
#end
Any help is much appreciated.
Thank you.
According to Apple's documentation
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.
You need to set userInteractionEnabled to YES on your UIImageView like this:
[image setUserInteractionEnabled:YES];
Hope this help.
You posted the complete example code of https://github.com/jaydee3/JDDroppableView/.
Perhaps you just ask a single question and refer to the project. But to use it for UIImageViews, you need to alloc init a JDDroppableView and put your imageView on it.
UIImageView *imageView = [[UIImageView alloc] initWithImage:...];
imageView.autoresizingMask = UIAutoresizingFlexibleWidth | UIAutoresizingFlexibleHeight;
JDDroppableView *droppableView = [[JDDroppableView alloc] initWithFrame:imageView.bounds];
[droppableView addSubview:imageView];
I have text on label which is not appearing completely so i want text to scroll on label from left to right. Please don't suggest numberoflines property as a solution.
FWIW, I did something similar using 2 labels in a scroll view...
#import <UIKit/UIKit.h>
#interface ScrolledTextView : UIScrollView
{
UILabel *label1;
UILabel *label2;
NSString *_text;
NSString *newText;
CGFloat textWidth;
BOOL animating;
BOOL needToUpdateText;
}
#property (nonatomic, copy, setter = setText:) NSString *text;
#property (nonatomic, assign) NSTimeInterval scrollInterval;
#property (nonatomic, assign) NSTimeInterval scrollDuration;
- (id) initWithFrame:(CGRect)frame andText : (NSString*) text;
- (void) setText:(NSString *)text;
- (BOOL) textFitsInFrame;
#implementation ScrolledTextView
#synthesize text = _text;
#synthesize scrollDuration;
#synthesize scrollInterval;
- (void) adjustLabelsForNewText : (NSString*) text andParentFrame : (CGRect) frame
{
_text = [NSString stringWithFormat:#"%# ", text];
CGSize textSize = [[self text] sizeWithFont:[label1 font] constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:UILineBreakModeClip];
[self setContentSize:CGSizeMake(textSize.width * 2, textSize.height)];
textWidth = textSize.width;
[label1 setFrame:CGRectMake(0.0, 0.0, textWidth, frame.size.height)];
[label1 setText:[self text]];
if([self textFitsInFrame])
{
[label2 removeFromSuperview];
}
else
{
[label2 setFrame:CGRectMake([label1 frame].size.width, 0.0, textWidth, frame.size.height)];
[label2 setText:[self text]];
if( ! [[self subviews] containsObject:label2])
{
[self addSubview:label2];
}
[self beginScrolling];
}
}
- (id) initWithFrame:(CGRect)frame andText : (NSString*) text
{
self = [super initWithFrame:frame];
if (self) {
[self setClipsToBounds:YES];
animating = NO;
needToUpdateText = NO;
[self setScrollDuration:10.0];
[self setScrollInterval:2.0];
label1 = [UILabel new];
label2 = [UILabel new];
[label1 setBackgroundColor:[UIColor clearColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[self addSubview:label1];
[self adjustLabelsForNewText:text andParentFrame:frame];
}
return self;
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if(needToUpdateText)
{
animating = NO;
needToUpdateText = NO;
[self adjustLabelsForNewText:#"" andParentFrame:[self frame]];
[self adjustLabelsForNewText:newText andParentFrame:[self frame]];
newText = nil;
return;
}
[self setContentOffset:CGPointMake(0.0, 0.0)];
[self beginScrolling];
}
- (void) beginScrolling
{
animating = YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[self scrollDuration]];
[UIView setAnimationDelay:[self scrollInterval]];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[self setContentOffset:CGPointMake(textWidth, 0.0)];
[UIView commitAnimations];
}
- (BOOL) textFitsInFrame
{
return textWidth <= [self frame].size.width;
}
- (void) setText:(NSString *)text
{
if(animating)
{
newText = text;
needToUpdateText = YES;
}
else
{
[self adjustLabelsForNewText:text andParentFrame:[self frame]];
}
}
if u appear a complete string in label using this line
CGSize labelsize = [label.text sizeWithFont:label.font];
then in CGRectMark(10,10, label.width,100 );
marquee are based on scroll content-size,if u set content-size(200,300),mean take that value to compare with x and y value to set a marquee.
regard
CNSivakumar
I have a problem with touchesBegan event.I displayed an image in an uiimageview.Am trying to detect the touches on that image by displaying small green color points on the image when i clicked on the image..
Am not getting any errors while running but the touches event is not firing.Here is my code:
#import <UIKit/UIKit.h>
#import "Second.h"
#interface pickerExampleViewController : UIViewController <UIImagePickerControllerDelegate>{
IBOutlet UIButton *selectPic;
}
#property (nonatomic,retain) UIButton *selectPic;
-(IBAction)getpic:(id)sender;
#end
#import "pickerExampleViewController.h"
#implementation pickerExampleViewController
#synthesize selectPic;
-(IBAction)getpic:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.editing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
#pragma mark imagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *) picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
Second *secview = [[Second alloc] initWithNibName:#"Second" bundle:nil];
secview.view.backgroundColor = [UIColor blackColor];
[secview setImage:image];
[self.view addSubview:secview.view];
//[self presentModalViewController:secview animated:YES];
[secview release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
#interface Second : UIViewController {
IBOutlet UIImageView *imgView;
UIImage *image1;
}
#property(nonatomic,retain) UIImageView *imgView;
-(void)setImage:(UIImage *)img;
-(IBAction)back;
#end
#import "Second.h"
#import <QuartzCore/QuartzCore.h>
#implementation Second
#synthesize imgView;
static int countoftouches,i;
CGPoint points[4];
-(void)setImage:(UIImage *)img
{
[imgView setImage:img];
countoftouches=0;
}
-(IBAction)back
{
[self.view removeFromSuperview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//NSSet *allTouches = [event allTouches];
if ([touch view] != imgView) {
return;
}
countoftouches++;
CGPoint point = [touch locationInView:imgView];
NSLog(#"x: %f, y: %f", point.x, point.y);
CGRect frame=CGRectMake(point.x, point.y, 5, 5);
if(countoftouches<=4)
{
points[i] = frame.origin;
i++;
UIButton *btn=[[UIButton alloc]initWithFrame:frame];
[btn setBackgroundColor:[UIColor greenColor]];
[imgView addSubview:btn];
[btn release];
}
}
-(void)dealloc
{
[imgView release];
[super dealloc];
}
#end
You need to set userInteractionEnabled to YES.. On youre UIImageView that is.. So:
imgView.userInteractionEnable = YES;