Can't animate a custom view - cocoa

I'm trying to animate the movement of a custom view of mine using the following code (my best translation from objective-c from Apples own documents)
var animDict = new NSMutableDictionary ();
animDict [NSViewAnimation.TargetKey] = this.View;
animDict [NSViewAnimation.StartFrameKey] = NSValue.FromRectangleF (this.View.Frame);
animDict [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (new RectangleF (0, 150 * index, 400, 150));
var theAnim = new NSViewAnimation ();
theAnim.SetValuesForKeysWithDictionary (animDict);
theAnim.Duration = 2;
theAnim.StartAnimation ();
However I get the following run-time error when running the app: 2011-05-08 00:53:30.827 EpisodeNext[61715:613] [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key NSViewAnimationTargetKey.
Any idea what I'm doing wrong? Thanx!

You cannot use the method SetValuesForKeysWithDictionary to pass the animation keys to the NSViewAnimation instance.
You must use the NSViewAnimation(NSDictionary[] viewAnimations) constructor to pass the animation dictionary.

// Create the attributes dictionary for the first view.
NSMutableDictionary firstViewDict = new NSMutableDictionary ();
//var firstViewRect = new NSDictionary[] { };
firstViewDict [NSViewAnimation.TargetKey] = animateWindow;
firstViewDict [NSViewAnimation.StartFrameKey] = NSValue.FromRectangleF (new RectangleF (1000, 300 , this.Frame.Width - 40, 0));
firstViewDict [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (SettingNSWindow.Frame);
//var NSDict = new NSDictionary[]{ };
NSDictionary[] NSDict = new NSDictionary[]{firstViewDict};
//NSDict.Cast(NSMutableDictionary = (NSDictionary[])firstViewDict;
NSViewAnimation theAnim = new NSViewAnimation(NSDict);
//theAnim.SetValuesForKeysWithDictionary (firstViewDict);
theAnim.Duration = 2;
theAnim.StartAnimation ();

Related

Urhosharp Material.FromImage not woking with some jpg files

I'm using Xamarin.Forms with Urhosharp in my project. I'm tring to set a matrial from an image on a sphere, everything is OK in my Android project but in iOS project, when I set material from some jpg files it doesn't work and all I get is a black screen.
Here is the jpg that works correctly:
And here is the other one that doesn't:
This is my code:
var scene = new Scene();
scene.CreateComponent<Octree>();
// Node (Rotation and Position)
var node = scene.CreateChild("room");
node.Position = new Vector3(0, 0, 0);
//node.Rotation = new Quaternion(10, 60, 10);
node.SetScale(1f);
// Model
var modelObject = node.CreateComponent<StaticModel>();
modelObject.Model = ResourceCache.GetModel("CustomModels/SmoothSphere.mdl");
var zoneNode = scene.CreateChild("Zone");
var zone = zoneNode.CreateComponent<Zone>();
zone.SetBoundingBox(new BoundingBox(-300.0f, 300.0f));
zone.AmbientColor = new Color(1f, 1f, 1f);
//get image from byte[]
//var url = "http://www.wsj.com/public/resources/media/0524yosemite_1300R.jpg";
//var wc = new WebClient() { Encoding = Encoding.UTF8 };
//var mb = new MemoryBuffer(wc.DownloadData(new Uri(url)));
var mb = new MemoryBuffer(PanoramaBuffer.PanoramaByteArray);
var image = new Image(Context) { Name = "MyImage" };
image.Load(mb);
//or from resource
//var image = ResourceCache.GetImage("Textures/grave.jpg");
var isFliped = image.FlipHorizontal();
if (!isFliped)
{
throw new Exception("Unsuccessful flip");
}
var m = Material.FromImage("1.jpg");
m.SetTechnique(0, CoreAssets.Techniques.DiffNormal, 0, 0);
m.CullMode = CullMode.Cw;
//m.SetUVTransform(Vector2.Zero, 0, 0);
modelObject.SetMaterial(m);
// Camera
var cameraNode = scene.CreateChild("camera");
_camera = cameraNode.CreateComponent<Camera>();
_camera.Fov = 75.8f;
_initialZoom = _camera.Zoom;
// Viewport
Renderer.SetViewport(0, new Viewport(scene, _camera, null));
I already tried to change compression level, ICCC profile and ...
I asked the same question in forums.xamarin.com and someone answered the question and I'll share it here :
In iOS every texture needs to have a power of two resolution, like 256 x 256 or 1024 x 512. Check if that is the issue. Additionally check that your using the latest UrhoSharp version.
Also make sure that the image is set as BundleResource in the iOS project.

AVCapturePhotoSettings not accepting accept NSDictionary element

not sure what I am doing wrong, I wanna create a simple custom camera, I'm creating the AVCapturePhotoOutput attaching it to AVCaptureSession, then creating AVCapturePhotoSettings with minimum settings to make taking a picture work, see code below.
I get exception kCVPixelBufferPixelFormatTypeKey is not being define but it is indeed in the NSDictionary I am passing.
I need some light here, thanks
public void TakePicture()
{
var output = new AVCapturePhotoOutput();
_captureSession.AddOutput(output);
var settings = AVCapturePhotoSettings.Create();
var previewPixelType = settings.AvailablePreviewPhotoPixelFormatTypes.First();
var keys = new[]
{
new NSString("kCVPixelBufferPixelFormatTypeKey"),
new NSString("kCVPixelBufferWidthKey"),
new NSString("kCVPixelBufferHeightKey"),
};
var objects = new NSObject[]
{
// don't have to be strings... can be any NSObject.
previewPixelType,
new NSString("160"),
new NSString("160")
};
var dicionary = new NSDictionary<NSString, NSObject>(keys, objects);
settings.PreviewPhotoFormat = dicionary;
output.CapturePhoto(settings,this);
}
It is because kCVPixelBufferPixelFormatTypeKey is not available in Xamarin.
We should use CVPixelBuffer.PixelFormatTypeKey here . It will be convert to kCVPixelBufferPixelFormatTypeKey automatically when compiling.
The same reason for kCVPixelBufferWidthKey and kCVPixelBufferHeightKey , the api is CVPixelBuffer.WidthKey and CVPixelBuffer.HeightKey in Xamarin.iOS.

How to insert new cell at first element in UICollectionView in Xamarin?

I want to make horizontal collection and add new cell at the first position.
I make custom UICollectionViewFlowLayout and override
LayoutAttributesForElementsInRect like this (to see reverse order of items):
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
var attributes = new UICollectionViewLayoutAttributes[cellCount];
NSIndexPath indexPath;
// Setting first position
int shift = 25, reverseXCenter;
for (int i = 0; i < cellCount; i++)
{
indexPath = NSIndexPath.FromItemSection(i, 0);
attributes[i] = LayoutAttributesForItem(indexPath);
// We have to make opposite order
reverseXCenter = (cellCount -1 - i) * 50;
attributes[i].Center = new CGPoint(reverseXCenter + shift, attributes[i].Center.Y);
}
return attributes;
}
I think is bad solution, because when I have more than 5 cells, the displayed collection is filled and I can't scroll.
I had debugger in GetOrCreateCellFor in CollectionViewSource
and indexPath.Item's had value for example (10,9,8,7,6...), where 10 is the last I added.
Maybe is another way to do this, or just I miss something?
Ok I am gonna help you out with a flow .
Suppose you are in a ViewController called "A" which will present a controller ViewController called "B"
Inside A use this function to push B
Public void PushNewHorizontalCollectionController(){
using (var vc =
new B(this.PageViewControllerLayout(), indexPath))
{
this.controller.PushViewController(pageViewController, true);
}
}
public UICollectionViewFlowLayout PageViewControllerLayout()
{
var flowLayout = new UICollectionViewFlowLayout();
var itemSize = this.controller.NavigationBarHidden ? new CGSize(Constants.ScreenWidth, Constants.ScreenHeight + 20) : new CGSize(Constants.ScreenWidth, Constants.ScreenHeight - Constants.NavigationHeaderAndStatusbarHeight);
flowLayout.ItemSize = itemSize;
flowLayout.MinimumLineSpacing = 0;
flowLayout.MinimumInteritemSpacing = 0;
flowLayout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;
return flowLayout;
}
Here itemSize is CGSize which u can assign what size you can ignore my constants which are self explainable .
Inside B your constructor should look like this .
public HorizontalViewController(UICollectionViewLayout layout, NSIndexPath indexpath) : base(layout)
{
Do your initialisation like registering UICollectionViewCell class nibs for use with this Controller
}
Handling real requirement from comment .
You wanna add something always as 1st element in collectionView ,Go to this method which should look like below
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
Console.WriteLine("getcellHori");
var isQuoteType = Photo.PhotoCollection[(int)indexPath.Item].Type;
if (isQuoteType == 3 || isQuoteType == 4)
{
var item = Photo.PhotoCollection[(int)indexPath.Item];
var cell = (HorizontalviewForQuoteType)collectionView.DequeueReusableCell("HorizontalviewForQuoteType", indexPath);
cell.PopulateCell(item.Caption,item.TextLabel,item.Likes,item.TimeLabel,item.Id);
HorizontalviewForQuoteType.pullAction = (offset) =>
{
this.pullOffset = offset;
this.NavigationController.PopViewController(true);
};
cell.SetNeedsLayout();
return cell;
}
Here you this line
var item = Photo.PhotoCollection[(int)indexPath.Item];
this is the data source , so whenever you adding something at index first and everything will be solved .

How to use TextButtonStyle.overFont?

I'm trying to use overFont and overColor on TextButton so the button appearance can changes when the mouse move over.
Here's the style I defined.
var buttonStyle = new TextButtonStyle();
buttonStyle.fontColor = new Color(1f, 1f, 1f, 1f);
buttonStyle.disabledFontColor = new Color(0, 0, 0, 0.4f);
buttonStyle.down = skin.getDrawable( "button_down");
buttonStyle.up= skin.getDrawable( "button_up");
buttonStyle.over= skin.getDrawable( "button_over");
buttonStyle.overFontColor = new Color(0, 0.3f, 0.3f, 1f);
buttonStyle.font = font
skin.add("default", buttonStyle);
The button is created as follows:
var startGameButton = new TextButton("Start game", skin);
startGameButton.x = buttonX;
startGameButton.y = currentY;
startGameButton.width = BUTTON_WIDTH;
startGameButton.height = BUTTON_HEIGHT;
stage.addActor(startGameButton);
/*startGameButton.addListener ([ Event e |
Gdx.app.log ("App", "Start game") ;
return true ;
])*/
startGameButton.addListener (new ChangeListener () {
override changed(ChangeEvent event, Actor actor) {
Gdx.app.log ("App", "Start game") ;
}
})
While the down and up state are properly taken into account, the over properties are not used: the button doesn't change when the mouse enters the button area.
buttonStyle.fontOverColor = Color.BLUE; works fine for me,
try to pass to your TextButton constructor not skin, but buttonStyle,
in TextButton there is such constructor
public TextButton (String text, TextButtonStyle style)
It's difficult to say something else, because code not looks like real working code, I mean var keyword or this code is not correct (there is no public variables x, y, width, height):
startGameButton.x = buttonX;
startGameButton.y = currentY;
startGameButton.width = BUTTON_WIDTH;
startGameButton.height = BUTTON_HEIGHT;
If changing constructor will not help you, please post your real code.

EaselJS: Using updateCache() with AlphaMaskFilter When Dragging Mask

I'm using an imported png with an alpha gradient that I'm setting as a mask that reveals the bitmap it is assigned to. The mask object is draggable (kind of like a flashlight). I know I'm supposed to use an AlphaMaskFilter as one of the filters, and I know I'm supposed to use .updateCache()... I'm just not sure I'm using them correctly?
var stage;
var assetQueue;
var bg;
var bgMask;
var container;
var amf;
$(document).ready(function(){
loadImages();
});
function loadImages()
{
// Set up preload queue
assetQueue = new createjs.LoadQueue();
assetQueue.addEventListener("complete", preloadComplete);
assetQueue.loadManifest([{id:"img_bg",src:"images/Nintendo-logo-red.jpg"}, {id:"img_bg_mask",src:"images/background_mask.png"}]);
}
function preloadComplete()
{
assetQueue.removeEventListener("complete", preloadComplete);
init();
}
function init()
{
stage = new createjs.Stage("stage_canvas");
setBackgrounds();
sizeStage();
$(document).mousemove(function(evt){
trackMouse(evt);
});
}
function trackMouse(evt)
{
var mouseX = evt.pageX;
var mouseY = evt.pageY;
// Move the containing clip around
container.x = mouseX - (bgMask.image.width / 2);
container.y = mouseY - (bgMask.image.height / 2);
// Offset the position of the masked image.
bg.x = -container.x;
bg.y = -container.y;
container.updateCache();
stage.update();
}
function setBackgrounds()
{
bg = new createjs.Bitmap(assetQueue.getResult("img_bg"));
bgMask = new createjs.Bitmap(assetQueue.getResult("img_bg_mask"));
container = new createjs.Container();
container.addChild(bg);
amf = new createjs.AlphaMaskFilter(bgMask.image)
container.filters = [amf];
container.cache(0, 0, bg.image.width, bg.image.height);
stage.addChild(container);
stage.update();
}
function sizeStage()
{
var windowW = 600;
var windowH = 600;
stage.canvas.width = windowW;
stage.canvas.height = windowH;
stage.update();
}
Solution found (for anyone interested). The key is to add the image you want to mask to a container. Move the container to any position you want, then offset the contained image within the container. The code has been updated to reflect this.

Resources